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
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:
- How to parse .log file and insert into database in PHP?Best solution by unix.com
- How to access nested array value?Best solution by stackoverflow.com
- How to parse JSON from String?Best solution by Stack Overflow
- Android: How to parse JSON file with Gson Library?Best solution by Stack Overflow
- How to parse inner array in JSON response with PHP?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.