0

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"]);
4
  • Present the code you've writted Commented Feb 16, 2016 at 9:58
  • Why not use a third party library/pod to help you out with the parsing? Commented Feb 16, 2016 at 10:06
  • 1
    Try replacing your code with the code provided by me. Commented Feb 16, 2016 at 10:17
  • Possible duplicate of How do I parse JSON with Objective-C? Commented Feb 16, 2016 at 10:41

8 Answers 8

2

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

    }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

2 Comments

value is not an array in the example given.
yes, thanks. But it thought he wanted to know how to do it in javascript so my answer is wrong anyways
1

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

Comments

0
NSDictionary *response = //Your json
NSArray *details = response[@"value"][@"details"]

etc. Pretty easy

Comments

0

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

Comments

0

I think your problem is that you have:

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

But it should be:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.