Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 解析JSON可以阅读各种字典_Ios_Objective C_Json_Afnetworking 2 - Fatal编程技术网

Ios 解析JSON可以阅读各种字典

Ios 解析JSON可以阅读各种字典,ios,objective-c,json,afnetworking-2,Ios,Objective C,Json,Afnetworking 2,我是json新手,遇到了一些困难,我正在尝试解析以下json 但是我不能阅读各种各样的字典 [{"project": { "id": 123, "name": "Produce RestKit Sample Code", "description": "We need more sample code!", "user": { "id": 1, "name": "Blake Watters", "email": "blake@twotoasters.com" }, "ta

我是json新手,遇到了一些困难,我正在尝试解析以下json

但是我不能阅读各种各样的字典

[{"project": {
"id": 123,
"name": "Produce RestKit Sample Code",
"description": "We need more sample code!",
"user": {
    "id": 1,
    "name": "Blake Watters",
    "email": "blake@twotoasters.com"
},
"tasks": [
    {"id": 1, "name": "Identify samples to write", "assigned_user_id": 1},
    {"id": 2, "name": "Write the code", "assigned_user_id": 1},
    {"id": 3, "name": "Push to Github", "assigned_user_id": 1},
    {"id": 4, "name": "Update the mailing list", "assigned_user_id": 1}
]}},
{"project": {
"id": 456,
"name": "Document Object Mapper",
"description": "The object mapper could really use some docs!",
"user": {
    "id": 2,
    "name": "Jeremy Ellison",
    "email": "jeremy@twotoasters.com"
},
"tasks": [
    {"id": 5, "name": "Mark up methods with Doxygen markup", "assigned_user_id": 2},
    {"id": 6, "name": "Generate docs and review formatting", "assigned_user_id": 2},
    {"id": 7, "name": "Review docs for accuracy and completeness", "assigned_user_id": 1},
    {"id": 8, "name": "Publish to Github", "assigned_user_id": 2}
]}}]
我正在使用AFNETWorking,以下是我的代码:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
for (NSDictionary *json in [responseObject objectForKey:@"project"]){
        simProjects *proj = [[simProjects alloc] init];
        proj.description = [json objectForKey: @"description"];

如前所述,您确实有一系列字典。你想要:

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSArray *projects = responseObject;
    for (NSDictionary *data in projects) {
        NSDictionary *project = data[@"project"];
        simProjects *proj = [[simProjects alloc] init];
        proj.description = project[@"description"];
    }
}

您收到的JSON绝对是一个数组。它是一个NSDictionary数组,每个NSDictionary都有一个密钥项目。