Kgrover
Kgrover

Reputation: 2116

Parsing JSON objective c

I'm initiating a URL connection and my script is returning the following JSON:

( 1, { "id" = <-ID Here->; hash = <-Hash Here->; }, ( ), ( ) )

All the examples I've seen so far seem to have "ids" or identifiers before respective arrays/dictionaries. Despite searching around, I couldn't find a way to parse this. (i.e. I need to get the first boolean value, the id, the hash, and then the arrays as well (which are empty as of now)).

Sorry if I'm missing something-- I'm new to parsing JSON in Obj-C.Thanks for the help.

Upvotes: 0

Views: 184

Answers (2)

Chris Trahey
Chris Trahey

Reputation: 18290

The feature I use is built in with the Cocoa libraries: the NSJSONSerialization class. It provides methods for parsing JSON into a graph, and encoding a graph into JSON. The rules are similar to plists (i.e. basic types plus arrays and dictionaries).

If you have NSData (which you can easily get from a string), you do like this:

NSArray *yourJSONAsObjectGraph = [NSJSONSerialization JSONObjectWithData:yourNSData options:nil error:&err];

Then, with your data above, objectAtIndex:0 would be an NSNumber that you can call boolValue on, objectAtIndex:1 will be an NSDictionary that you can call objectForKey:@"id" (and @"hash"), etc.

Upvotes: 2

DrummerB
DrummerB

Reputation: 40211

See How to use JSON in Objective-C. If you didn't hear about it yet, have a look at SBJSON.

Upvotes: 0

Related Questions