how to get objects from a json array in iphone?
If your target SDK is ios4 or higher, you can use this project
https://github.com/stig/json-framework/
Once you add the source to your project, just
#import "SBJson.h"
and convert your Json string as follows
jsonResponse = [string JSONValue];
The method will fail if you don't have the full Json array in your string, but you can keep appending strings until it doesn't fail
To follow up for codejunkie's request belowyou can assume in your data structure that the jsonResponse
is an NSArray
In other implementations take care to test the response for NSArray or NSDictionary
NSArray * myPeople = [string JSONValue]; NSMutableDictionary * organizedData = [[NSMutableDictionary alloc] init];for (NSDictionary * p in myPeople) { [organizedData setValue:p forKey:[p valueForKey:@"id"]];} // now you can query for an id like so // [organizedData valueForKey:@"1"]; and your output will be what you wanted from the original question // just don't forget to release organizedData when you are done with it
https://github.com/johnezang/JSONKit
I use this to get data from a webservice that spits out 50 records each having another 20 internal elements similar to the one you specify...
I use the JSONKit in the following manner..(Had a look at SBJson a lot of user but i got confused from the word go.)
JSONDecoder *jArray = [[JSONDecoder alloc]init];NSMutableArray *theObject = [[NSMutableArray alloc] init]; theObject = [jArray objectWithData:theResponseData];//objectWithString:theResponseStringNSMutableArray *csArray = [[NSMutableArray array] retain] ; for(id key in theObject) { if([key valueForKey:@"firstName"] != Nil) { ........ } if([key valueForKey:@"lastName"] != Nil) { ........ } }
check it out and let me know if it works or not.. By the way Great responses guys... Good