简体   繁体   中英

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. (ie 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.

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 (ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM