简体   繁体   中英

Parsing nested JSON entries

So far i've only ever parsed JSON that was on an initial array but am unsure how to proceed.

Here is my JSON:

{
    "SongDeviceID": [
        {
            "SID": "714",
            "SDID": "1079287588763212246"
        },
        {
            "SID": "715",
            "SDID": "1079287588763212221"
        },
        {
            "SID": "716",
            "SDID": "1079287588763212230"
        }
    ]
}

And here is what I have so far in my code:

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonResponse options: NSJSONReadingMutableContainers error: &e];
NSArray * responseArr = [NSArray arrayWithObject:jsonResponse];
for (NSDictionary *dict in responseArr)

I think im going about this the wrong way because im used to having only one layer deep JSON responses, can somebody help me out?

You are close. You need:

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonResponse options: NSJSONReadingMutableContainers error: &e];
NSArray * responseArr = jsonArray[@"SongDeviceID"];
for (NSDictionary *dict in responseArr) {
    // dict has two keys - SID and SDID
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM