How do I parse nested JSON object?

iOS 5 Google JSON - How to parse nested object?

  • I Am trying to parse a Youtube playlist: If my JSON is structured like: {"apiVersion" .... "items":[{"id2":"some-id","title":"songtitle", I am perfectly able to parse the title via: // Fill array NSArray *items = [json objectForKey:@"items"]; // Get item from tableData NSDictionary *item = [tableData objectAtIndex:[indexPath row]]; // Set text on textLabel [[cell textLabel] setText:[item objectForKey:@"title"]]; But if the JSON is like: {"apiVersion" .... "items":[{"id1": .... "video":{"id2":"some-id","title":"songtitle", How can i reach the nested object title? Just a simple thing, but i am banging my head on this for hours now. Gets frustrating, thanks for your suggestions! [EDIT] This is the full structure: { "items": [ { "video": { "title": "Number One", "description": "Description one" }, { "title": "Number two", "description": "Description two" }, { "title": "Number three", "description": "Description three" } }, { "video": { "title": "Number One", "description": "Description one" }, { "title": "Number two", "description": "Description two" }, { "title": "Number three", "description": "Description three" } } ] }

  • Answer:

    The problem is that your json is not valid. All the titles should be in array right? So they have to be in [ ]. You can use for example the following site to "debug" your json: http://jsonformatter.curiousconcept.com/ Also, in iOS 5 you can use the new built-in JSON api. Here is a nice tutorial for that: http://www.raywenderlich.com/5492/working-with-json-in-ios-5 Nevertheless, here is how your json should look like I guess: { "items":[ { "video":[ { "title":"Number One", "description":"Description one" }, { "title":"Number two", "description":"Description two" }, { "title":"Number three", "description":"Description three" } ] }, { "video":[ { "title":"Number One", "description":"Description one" }, { "title":"Number two", "description":"Description two" }, { "title":"Number three", "description":"Description three" } ] } ] } Good luck with your project ;)

Martindem at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

NSMutableArray *items = [json valueForkey:@"items"]; for(int i =0;i<[items count]; i++) { NSMutableArray *arrtitle = [[[items objectAtIndex:i]valueForkey:@"Video"]copy]; for(int j =0;j<[arrtitle count]; j++) { NSString *title = [[arrtitle objectAtIndex:j]valueForkey:@"title"]; } } maybe it will help you.

Chinttu

Try This: NSArray *items = [[json valueForkey:@"video"]valueForkey:"items"];

mihirios

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.