Tuesday, May 24, 2011

JSON parsing in iphone

JSON (JavaScript Object Notation) is a lightweight data-interchange format .But how do i parse json in iphone,its fairly simple thanks to the json-framework http://code.google.com/p/json-framework/
Steps Required
1.Download the library from the above link.
2.In your project Drag and drop the JSON folder from the downloaded folder.Check the option copy folders.
3.Import JSON.h to the file where you want to do the json parsing.
Now for the parsing its fairly simple
If your json Format is an object then you can parse directly

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSDictionary *results = [jsonString JSONValue];

NSString *studentsAge=[results objectForKey:@"age"];


Depending on your object you have to adjust the result variable data type and You were done.