简体   繁体   中英

How to parse such a json array?

Im having hard time while trying to parse the following json array. 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. Please check this, How to convert NSDictionary to custom object .

And yes, I'm using this site to check my json response.

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!

You can parse your json data with:

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. So you should read the value dict and within that read the details array.

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. 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.

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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