ios - 解析嵌套的 JSON 条目

标签 ios objective-c json

到目前为止,我只解析过初始数组中的 JSON,但不确定如何继续。

这是我的 JSON:

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

这是我目前的代码:

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

我认为我以错误的方式解决这个问题,因为我习惯于只有一层深度 JSON 响应,有人可以帮我吗?

最佳答案

你很接近。你需要:

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
}

关于ios - 解析嵌套的 JSON 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18415329/

相关文章:

web-services - 计算 MAP 中两点之间的距离(以公里为单位)

iphone - 将图像文件上传到 Web 表单

iphone - 在iPhone上使用地址解析检查地址的有效性

json - 从将Json字符串转换为Struct解析错误

php - 来自一个 AJAX 请求的多个响应

javascript - 抓取嵌套 Json 对象深处的数据

ios - 无法让 iOS Print Renderer 正确绘制

android - 用户必须设置安全屏幕锁定 Ionic 2 安全存储

iOS 图表不会将 x 轴置于 y 轴上的零

ios - 在 UIButtons 上全局设置背景颜色