Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
KARTHIK GUTTAPUDI
1,292 PointsJSON to NSDictionary
How does the first line of code convert JSON data to NSDictionary objects, can someone please explain these two lines of code
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSArray *blogPostArray = [jsonDictionary objectForKey:@"posts"];
1 Answer
Amit Bijlani
Treehouse Guest TeacherThe NSJSONSerialization class contains the logic to convert JSON data into NSArray or NSDictionary. The second line is simply retrieving an array from the key posts of the jsonDictionary. Here's an explanation of NSDictionary
KARTHIK GUTTAPUDI
1,292 PointsKARTHIK GUTTAPUDI
1,292 PointsPlease correct me If I am wrong, the entire json response/data is converted into a single NSDictionary object, right? How is the data inside the posts key is also converted to an array of NSDictionary objects?
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherYou are right. If you see the documentation for
NSJSONSerializationyou will see that it says:The data inside the
postskey is converted to an array as part of the JSON parsing performed by theNSJSONSerializationclass.KARTHIK GUTTAPUDI
1,292 PointsKARTHIK GUTTAPUDI
1,292 Pointsok got it, thank you. Any idea when can we expect some tutorials on iphone/ipad game development.