JSON parsing on iPhone SDK

后端 未结 5 1297
灰色年华
灰色年华 2021-01-17 01:29

I have a certain json:

[
    {
        \"id\"        : 42422422,
        \"created\"   : 1329684013,
        \"name\"          : \"Test\"
    },
    {
               


        
5条回答
  •  情歌与酒
    2021-01-17 01:35

    With your non-error JSON payload you have a top level Array (noted by the beginning [ ), whilst with your error JSON payload you don't.

    {
        "error" : {
            "code"      : "511",
            "message"   : "JSON error",
            "extra" : {
                "some"      : "text",
                "someextra" : "text"
            }
        }
    }
    

    The error code is a nested dictionary object, therefore you would use the following to parse it:

    NSDictionary* json = [NSJSONSerialization
                                  JSONObjectWithData:responseData
                                  options:kNilOptions
                                  error:&error];
    
    NSString *errorCode = [NSString stringWithFormat:@"%@",[(NSDictionary*)[json objectForKey:@"error"]objectForKey:@"code"]];
    

提交回复
热议问题