Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS JSON NSString Parse

Tags:

json

ios

I have a JSON string as an NSString object in iOS. I want to parse this and pull out the given parameters in the JSON string. Is there a efficient way to parse this or is the only way to search for substrings etc.?

like image 904
user1120008 Avatar asked Apr 17 '12 23:04

user1120008


1 Answers

The way to do it with iOS 5 is to use the NSJSONSerialization class. You will want to first convert your string to an NSData object, and call the class method JSONObjectWithData

NSData *jsonData = [myJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];

Note that JSONObjectWithData will return either an NSDictionary or an NSArray, depending whether your JSON string represents an a dictionary or an array.

like image 150
jonkroll Avatar answered Sep 29 '22 00:09

jonkroll