3

I found this article about parsing JSON response from a URL request i iOS: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service.

The article uses JSON Framework. I've downloaded the newest "SBJson_v3.0.4.zip" from the webpage and dragged in into a new group in my project. But then the build returns 62 errors like:

  • ARC forbids explicit message send of 'release'
  • Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained
  • 'retain' is unavailable: not available in automatic reference counting mode

Does anybody know why? Is the JSON Framework not compatible with the newest Xcode?

dhrm
  • 14,335
  • 34
  • 117
  • 183
  • This has more to do with your OS target ( >= 5.0) than Xcode. Related: http://stackoverflow.com/questions/6368600/some-questions-about-automatic-reference-counting-in-ios5-sdk/6368692#6368692 – peterp Nov 29 '11 at 18:45

4 Answers4

12

Since iOS 5, iOS has its own JSON parser (thank you Twitter!)

NSError *err = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];

Do make sure to check the type of the output - it can be anything from a string to a number to a dictionary to an array.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • I upvoted both answers, but for the record, I like this one a lot more because why use an external framework if you don't have to? – Chuck Nov 29 '11 at 18:46
  • `JSONObjectWithData` is returning null. Why could it return null? Any idea? – Adil Malik Jun 01 '13 at 11:36
  • I found the problem. My jSon was not valid. Sorry, I am new to iOS. Finding debugging a little difficult as compared to Android. – Adil Malik Jun 01 '13 at 11:50
4

You need to disable the Automatic Reference Counting for those specific files provided in the package. If you go to the Project Settings -> Your Target -> Build Phases Tab and expand the Compile Sources arrow you'll see all your project files.

Under the ones that belong to SBJSON you need to add the -fno-objc-arc compiler flag (find the file, double click on the right bit of the table and it'll bring up a box where you can add the compiler flags)

SBJSON is compatible with the latest SDK but it is not compatible with the Automatic Reference Counting enabled in the latest SDK which is why you get these errors.

Here is an image of what it should look like

Suhail Patel
  • 13,644
  • 2
  • 44
  • 49
  • 3
    Wow I'd really like to know, how you name your files… – vikingosegundo Nov 29 '11 at 19:10
  • Hehe I took the screenshot from a (secret) personal project i'm working on and the files are namespaced with the App Name first in order to avoid class collisions. No other reason apart from that :) – Suhail Patel Nov 29 '11 at 19:14
1

The newest SBJSON has ARC support.

https://github.com/stig/json-framework

Sofi Software LLC
  • 3,879
  • 1
  • 36
  • 34
0

in your project settings, go to build phases and click on compile sources, you would get a bunch of .m files which are nothing but your project files. Click on the compiler flags colums for the corresponding file for which you need to disable ARC and type in -fno-objc-arc . Also, if your project doesn't supports ARC but you need to enable ARC in few files you can follow the same procedure but this time type in -fobjc-arc

Now you can use any json kit. No need to worry about the format. But i would strongly recommend to try out apple's inbuilt NSJSonSerialization methods and then go for third party json kits if not satisfied with Apple.

There are loads of them available on github along with their documentation . The one which i am currently using in my project is johnezang-jsonkit

Hope this helps you

madLokesh
  • 1,860
  • 23
  • 49