简体   繁体   English

如何解析这样的json数组?

[英]How to parse such a json array?

Im having hard time while trying to parse the following json array. 我在尝试解析以下json数组时遇到困难。 How to parse it. 如何解析。 The other answers in web doesn't seem to solve my problem. 网络中的其他答案似乎无法解决我的问题。

{
  "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"
      }
    ]
  }
}

I have to access the values of each of those keys. 我必须访问每个键的值。

Following is my code 以下是我的代码

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"]);

This is how you can parse your whole dictionary: 这是解析整个字典的方法:

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"];

    }
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"]];

//argsArray holds objects in form of NSDictionary.
for(NSDictionary *response in argsArray) {
   //String object
    NSLog(@"%@", [response valueForKey:@"shipment_ref_no"]);
   //Dictionary object
    NSLog(@"%@", [[response objectForKey:@"point_of_contact"] valueForKey:@"empid"]);
   //String object
    NSLog(@"%@", [response valueForKey:@"status"]);
   //Dictionary object
    NSLog(@"%@", [[response objectForKey:@"products"] valueForKey:@"0"]);
}

I believe you should surely ask your server developer to update the response format. 我相信您一定应该要求服务器开发人员更新响应格式。

Also, you can always use Model classes to parse your data. 另外,您始终可以使用Model类来解析数据。 Please check this, How to convert NSDictionary to custom object . 请检查一下如何将NSDictionary转换为自定义对象

And yes, I'm using this site to check my json response. 是的,我正在使用网站来检查我的json响应。

you can parse like ... 你可以解析...

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"]];
}

EDIT: Following answer is in javascript! 编辑:以下答案在javascript中!

You can parse your json data with: 您可以使用以下方式解析json数据:

var array = JSON.parse(data);

and then you can get everything like this: 然后您将获得如下所示的所有信息:

var refno = array["value"]["details"][0]["shipment_ref_no"];
NSDictionary *response = //Your json
NSArray *details = response[@"value"][@"details"]

etc. Pretty easy 等。很容易

Update your code as follows. 如下更新代码。 You are trying to read the details array from the top level whereas in your data its inside the value key. 您正在尝试从顶层读取details数组,而在您的数据中则是使用value键。 So you should read the value dict and within that read the details array. 因此,您应该阅读值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"]);

I think your problem is that you have: 我认为您的问题是您有:

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

But it should be: 但是应该是:

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

Below is code for parsing JSON array. 以下是解析JSON数组的代码。 i have used to parse JSON array from file but you can also do this using response link also.I have provided code for both and are below. 我曾经从文件中解析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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM