-1

我有以下由 NSURLconnection 和 NSJSONSerialization 解析的 json 代码:

{
  "JSonArray" : [
    {
      "JsonLog" : {
        "msg" : "test",
        "sno" : "1",
        "user" : "test1"
      }
    },
    {
      "JsonLog" : {
        "msg" : "test",
        "sno" : "2",
        "user" : "test2"
      }
    },
    {
      "JsonLog" : {
        "msg" : "test",
        "sno" : "3",
        "user" : "test3"
      }
    }
  ]
}   

我用过的代码:

NSError *requestError = NULL;

NSDictionary *allData=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:&requestError];


if (requestError){
    //An error occurred.
    NSLog(@"error is : %@",requestError);
}

if (! allData) {
    NSLog(@"Got an error: %@", requestError);
} else {
    NSLog(@" data is : %@",allData) ;
}



NSArray *arrayOfEntry=[allData objectForKey:@"JSonArray"];

for (NSDictionary *diction in arrayOfEntry) {

    NSDictionary *title=[diction objectForKey:@"JsonLog"] ;

    NSString *label=[title objectForKey:@"sno"];

    [array addObject:label];

}

NSLog(@" data is : %@",allData)的响应 是:

{
    JSonArray =     (
                {
            JsonLog =             {
                msg = "test";
                "sno" = 1;
                user = test1;
            };
        },
                {
            JsonLog =             {
               msg = "test";
                "sno" = 2;
                user = test2;
            };
        },
                {
            JsonLog =             {
                msg = "test";
                "sno" = 3;
                user = test3;
            };
        }
    );
}

我哪里出错了,我该如何正确设置?有什么我想念的吗?

任何建议都会对您有很大帮助。

提前致谢。

4

1 回答 1

1

我没有看到有关代码或行为的任何问题,因为 JSON 已正确解析。

更新:我尝试编译并运行您的代码,NSLog()在生成结果array变量后,它显示

(
    1,
    2,
    3
)

所以错误肯定在其他地方。

于 2013-03-16T09:17:14.930 回答