在ios中解析并推送嵌套的json对象

时间:2015-08-07 05:41:08

标签: ios json nested

我在ios中使用嵌套的Json对象,问题是无法解析它。任何人都可以帮助我解决这个问题。我遵循了许多链接但事实证明是富有成效的。

Json文件如下:

{

"taskList":[
    {
        "taskId":101,
        "taskName":"Survey Task1",
        "details":"australian map for field partners",
        "dropDate":"2014-11-26",
        "dueDate":"2014-11-27",
        "supervisorId":12,
        "supervisorContactInfo":"54543",
        "status":"assigned",
        "formList":[
            {
                "formId":"CAFvodafone",
                "formName":"CAF vodafone",

            },
            {
                "formId":"CAF",
                "formName":"CAF",

            },
            {
                "formId":"telenore_cdk",
                "formName":"telenore cdk",

            }
        ]
    },
    {
        "taskId":102,
        "taskName":"Survey Task2",
        "details":"australian map for field partners",
        "dropDate":"2014-11-26",
        "dueDate":"2014-11-27",
        "supervisorId":12,
        "supervisorContactInfo":"54543",
        "status":"assigned",
        "formList":[
            {
                "formId":"Salesform",
                "formName":"Sales form",

            }
        ]
    },
    {
        "taskId":111,
        "taskName":"Survey Task8",
        "details":"australian map for field partners",
        "dropDate":"2014-11-29",
        "dueDate":"2014-12-20",
        "supervisorId":12,
        "supervisorContactInfo":"54543",
        "status":"assigned",
        "formList":[
            {
                "formId":"CAF",
                "formName":"CAF",

            }
        ]
    },
    {
        "taskId":118,
        "taskName":"Survey Task8",
        "details":"australian map for field partners",
        "dropDate":"2014-11-29",
        "dueDate":"2014-12-20",
        "supervisorId":12,
        "supervisorContactInfo":"54543",
        "status":"assigned",
        "formList":[
            {
                "formId":"CAFvodafone",
                "formName":"CAF vodafone",

            }
        ]
    },
    {
        "taskId":113,
        "taskName":"Survey Task8",
        "details":"australian map for field partners",
        "dropDate":"2014-11-29",
        "dueDate":"2014-12-20",
        "supervisorId":12,
        "supervisorContactInfo":"54543",
        "status":"assigned",
        "formList":[
            {
                "formId":"CAF",
                "formName":"CAF",

            }
        ]
    },
    {
        "taskId":112,
        "taskName":"Survey Task8",
        "details":"australian map for field partners",
        "dropDate":"2014-11-29",
        "dueDate":"2014-12-20",
        "supervisorId":12,
        "supervisorContactInfo":"54543",
        "status":"assigned",
        "formList":[
            {
                "formId":"CAF",
                "formName":"CAF",

            }
        ]
    },
    {
        "taskId":116,
        "taskName":"Survey Task8",
        "details":"australian map for field partners",
        "dropDate":"2014-11-29",
        "dueDate":"2014-12-20",
        "supervisorId":12,
        "supervisorContactInfo":"54543",
        "status":"assigned",
        "formList":[
            {
                "formId":"CAFvodafone",
                "formName":"CAF vodafone",

            }
        ]
    }
]

作者:Roop

2 个答案:

答案 0 :(得分:1)

来自你的json词典

id arrayTrack = json[@"taskList"];

    if ([arrayTrack isKindOfClass:[NSArray class]]){ // check it array or not

        for (id trackDictionary in arrayTrack) {

            if ([trackDictionary isKindOfClass:[NSDictionary class]]){ //check it Dictionary or not

                trackId = trackDictionary[@"taskId"];
// Like this you can fetch other key values...
            }


        }

    }

我认为这会对你有所帮助:)。

答案 1 :(得分:0)

您尝试这种方式解析json数据

NSLog(@"%@",[[[yourjsonDictionary objectForKey:@"taskList"] objectAtIndex:0] objectForKey:@"taskName"]);

json响应返回字典首先从字典中获取值

[yourjsonDictionary objectForKey:@"taskList"] // this statement return array

你得到数组中的第一个键和值,并以这种方式获取数组值

[[yourjsonDictionary objectForKey:@"taskList"] objectAtIndex:0] // return first array element

获取0元素值

[[[yourjsonDictionary objectForKey:@"taskList"] objectAtIndex:0] objectForKey:@"taskName"] //return task name

[[[yourjsonDictionary objectForKey:@"taskList"] objectAtIndex:0] objectForKey:@"taskId"] // return task Id

获取嵌套的formList数据

[[[[[yourjsonDictionary objectForKey:@"taskList"] objectAtIndex:0] objectForKey:@"formList"] objectAtIndex:0] objectForKey:@"formName"] // return form name

OR

NSLog(@"%@",yourjsonDictionary[@"taskList"][0][@"taskName"]); // print task name