Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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_Iphone_Xcode - Fatal编程技术网

Ios 如何从JSON数组中获取值

Ios 如何从JSON数组中获取值,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,我可以知道如何以下面的代码格式从JSON获取响应值吗?目前我正在使用这种方式,但它对我不起作用 我使用什么来获取JSON值: NSError * err2 = nil; NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData options:0

我可以知道如何以下面的代码格式从JSON获取响应值吗?目前我正在使用这种方式,但它对我不起作用

我使用什么来获取JSON值:

NSError * err2 = nil;
NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData
                                                options:0
                                                  error:&err2];

NSMutableDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
我想要得到的JSON值:

 [
          {
            "Status": 1,
            "NearuCode": "17060521957",
            "ParcelBox": "Thursday"
          },
          {
            "Status": 1,
            "NearuCode": "17060521957",
            "ParcelBox": "Thursday"
          }
        ]

您的
JSON
响应是
Array
not
Dictionary
而且在您的响应中,数组中的两个对象都具有相同的数据

NSMutableArray *jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dic in jsonObject) {
     NSLog(@"%@", dic[@"Status"])
     NSLog(@"%@", dic[@"NearuCode"])
     NSLog(@"%@", dic[@"ParcelBox"])
}

我给你一个解决方案,它解释了当你得到空结果时错误的描述

NSError * err2 = nil;
NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData
                                            options:0
                                              error:&err2];
if (err2)
{
     NSLog(@"The reason for the error is - %@",[err2 description]);
}

问题是什么?@dahiya_boy响应没有显示我需要的JSON值want@Edmund如何检索值?哪个数组不给出响应?打印json和jsonObject数组结果json可以是数组或字典,因此您不能只将其分配给数组。这可能只适用于此答案,但最好将其放在id对象并检查其type@hariszaman你们不认为来自API的JSON将保持不变吗?若它是数组,那个么它也将保持在iOS端的数组不变。如果已经知道JSON为什么应该使用id而不是它的特定类型,那么如果他对不返回数组的API使用相同的代码呢?@hariszaman你读过这个问题了吗,OP已经添加了它的JSON响应,我在回答中也提到了,那么你的
JSON
响应是
array
而不是
dictionary
。首先OP没有得到响应。他得到的结果是空的。欢迎兄弟