Ios 如何解析这样的json数组?

Ios 如何解析这样的json数组?,ios,objective-c,json,Ios,Objective C,Json,我在尝试解析以下json数组时遇到了困难。如何解析它。网络上的其他答案似乎并不能解决我的问题 { "status": 1, "value": { "details": [ { "shipment_ref_no": "32", "point_of_contact": { "empid": "" }, "products": { "0": " Pizza"

我在尝试解析以下json数组时遇到了困难。如何解析它。网络上的其他答案似乎并不能解决我的问题

{
  "status": 1,
  "value": {
    "details": [
      {
        "shipment_ref_no": "32",
        "point_of_contact": {
          "empid": ""
        },
        "products": {
          "0": " Pizza"
        },"status": "2"
      },
      {
        "shipment_ref_no": "VAPL/EXP/46/14-15",
        "point_of_contact": {
          "empid": "60162000009888"
        },
        "products": {
          "0": "MAIZE/CORN STARCH"
        },
        "status": "5"
      }
    ]
  }
}
我必须访问每个键的值

下面是我的代码

NSString* pendingResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSData *jsonData = [pendingResponse dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSArray *argsArray = [[NSArray alloc] initWithArray:[jsonDic objectForKey:@"details"]];
NSDictionary *argsDict = [[NSDictionary alloc] initWithDictionary:[argsArray objectAtIndex:0]];
NSLog(@"keys = %@", jsonDic[@"values"]);

编辑:下面的答案是javascript

您可以使用以下方法解析json数据:

var array = JSON.parse(data);
然后你可以得到这样的一切:

var refno = array["value"]["details"][0]["shipment_ref_no"];

编辑:下面的答案是javascript

您可以使用以下方法解析json数据:

var array = JSON.parse(data);
然后你可以得到这样的一切:

var refno = array["value"]["details"][0]["shipment_ref_no"];
等等,很简单


等等。很容易更新代码,如下所示。您试图从顶层读取
详细信息
数组,而在数据中,它位于
键内。因此,您应该读取dict值,并在其中读取details数组

NSString* pendingResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *jsonData = [pendingResponse dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSArray *argsArray = [[NSArray alloc] initWithArray:[valueDict objectForKey:@"details"]];
NSDictionary *argsDict = [[NSDictionary alloc] initWithDictionary:[argsArray objectAtIndex:0]];
NSLog(@"keys = %@", jsonDic[@"values"]);

更新你的代码如下。您试图从顶层读取
详细信息
数组,而在数据中,它位于
键内。因此,您应该读取dict值,并在其中读取details数组

NSString* pendingResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *jsonData = [pendingResponse dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSArray *argsArray = [[NSArray alloc] initWithArray:[valueDict objectForKey:@"details"]];
NSDictionary *argsDict = [[NSDictionary alloc] initWithDictionary:[argsArray objectAtIndex:0]];
NSLog(@"keys = %@", jsonDic[@"values"]);

我认为你的问题在于:

NSLog(@"keys = %@", jsonDic[@"values"]);
但它应该是:

NSLog(@"keys = %@", jsonDic[@"value"]);

我认为你的问题在于:

NSLog(@"keys = %@", jsonDic[@"values"]);
但它应该是:

NSLog(@"keys = %@", jsonDic[@"value"]);
我相信您应该要求您的服务器开发人员更新响应格式

此外,您始终可以使用模型类来解析数据。请检查这个

是的,我正在使用这个站点检查我的json响应

我相信您应该要求您的服务器开发人员更新响应格式

此外,您始终可以使用模型类来解析数据。请检查这个


是的,我正在使用该站点检查我的json响应。

这是解析整个词典的方法:

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    NSArray *details = [[dataDictionary objectForKey:@"value"] objectForKey:@"details"];

    for (NSDictionary *dic in details) {
        NSString *shipmentRefNo = dic[@"shipment_ref_no"];
        NSDictionary *pointOfContact = dic[@"point_of_contact"];
        NSString *empId = pointOfContact[@"empid"];

        NSDictionary *products = dic[@"products"];
        NSString *zero = products[@"0"];
        NSString *status = dic[@"status"];

    }

以下是解析整个词典的方法:

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    NSArray *details = [[dataDictionary objectForKey:@"value"] objectForKey:@"details"];

    for (NSDictionary *dic in details) {
        NSString *shipmentRefNo = dic[@"shipment_ref_no"];
        NSDictionary *pointOfContact = dic[@"point_of_contact"];
        NSString *empId = pointOfContact[@"empid"];

        NSDictionary *products = dic[@"products"];
        NSString *zero = products[@"0"];
        NSString *status = dic[@"status"];

    }
你可以像解析

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *dictValue = [[NSDictionary alloc] initWithDictionary:[jsonDic objectForKey:@"value"]];

NSArray *arrDetails = [[NSArray alloc] initWithArray:[dictValue objectForKey:@"details"]];

for (int i=0; i<arrDetails.count; i++) 
{
    NSDictionary *dictDetails=[arrDetails objectAtIndex:i];
    NSDictionary *dictContact = [[NSDictionary alloc] initWithDictionary:[dictDetails objectForKey:@"point_of_contact"]];
    NSDictionary *dictProduct = [[NSDictionary alloc] initWithDictionary:[dictDetails objectForKey:@"products"]];
}
NSDictionary*jsonDic=[NSJSONSerialization JSONObjectWithData:jsonData选项:NSJSONReadingAllowFragments错误:nil];
NSDictionary*dictValue=[[NSDictionary alloc]initWithDictionary:[jsonDic objectForKey:@“value”];
NSArray*arrDetails=[[NSArray alloc]initWithArray:[dictValue objectForKey:@“details”];
对于(inti=0;i您可以像

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *dictValue = [[NSDictionary alloc] initWithDictionary:[jsonDic objectForKey:@"value"]];

NSArray *arrDetails = [[NSArray alloc] initWithArray:[dictValue objectForKey:@"details"]];

for (int i=0; i<arrDetails.count; i++) 
{
    NSDictionary *dictDetails=[arrDetails objectAtIndex:i];
    NSDictionary *dictContact = [[NSDictionary alloc] initWithDictionary:[dictDetails objectForKey:@"point_of_contact"]];
    NSDictionary *dictProduct = [[NSDictionary alloc] initWithDictionary:[dictDetails objectForKey:@"products"]];
}
NSDictionary*jsonDic=[NSJSONSerialization JSONObjectWithData:jsonData选项:NSJSONReadingAllowFragments错误:nil];
NSDictionary*dictValue=[[NSDictionary alloc]initWithDictionary:[jsonDic objectForKey:@“value”];
NSArray*arrDetails=[[NSArray alloc]initWithArray:[dictValue objectForKey:@“details”];

for(int i=0;i下面是解析JSON数组的代码。我曾经从文件中解析JSON数组,但您也可以使用响应链接来完成此操作。我已经为两者提供了代码,下面是

// using file
NSString *str = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [[NSData alloc]initWithContentsOfFile:str];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSMutableDictionary *dictValues = [[NSMutableDictionary alloc]initWithDictionary:[dict valueForKey:@"value"]];
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[dictValues valueForKey:@"details"] copyItems:YES];

NSLog(@"Array Details :- %@",array);

// using url
NSURL *url = [NSURL URLWithString:@"www.xyz.com"]; // your url
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSMutableDictionary *dictValues = [[NSMutableDictionary alloc]initWithDictionary:[dict valueForKey:@"value"]];
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[dictValues valueForKey:@"details"] copyItems:YES];

NSLog(@"Array Details :- %@",array);

下面是解析JSON数组的代码。我曾经从文件解析JSON数组,但您也可以使用响应链接来解析。我已经提供了这两个方面的代码,如下所示

// using file
NSString *str = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [[NSData alloc]initWithContentsOfFile:str];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSMutableDictionary *dictValues = [[NSMutableDictionary alloc]initWithDictionary:[dict valueForKey:@"value"]];
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[dictValues valueForKey:@"details"] copyItems:YES];

NSLog(@"Array Details :- %@",array);

// using url
NSURL *url = [NSURL URLWithString:@"www.xyz.com"]; // your url
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSMutableDictionary *dictValues = [[NSMutableDictionary alloc]initWithDictionary:[dict valueForKey:@"value"]];
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[dictValues valueForKey:@"details"] copyItems:YES];

NSLog(@"Array Details :- %@",array);

演示您编写的代码为什么不使用第三方库/pod帮助您完成解析?尝试用我提供的代码替换您的代码。演示您编写的代码可能重复为什么不使用第三方库/pod帮助您完成解析?尝试用我提供的代码替换您的代码。可能重复在给出的示例中,
value
的e不是数组。是的,谢谢。但它认为他想知道如何用javascript实现它,所以我的答案无论如何都是错误的
value
在给出的示例中不是数组。是的,谢谢。但它认为他想知道如何用javascript实现它,所以我的答案无论如何都是错误的