user3600801
user3600801

Reputation:

Trying to parse a json crashes the app

I have got the following json

 {"status":1,"value":{"details":{"40404000024769":[{"name":"","email":""}]}}}

Im parsing it as follows,

NSString *statusCode = [NSString stringWithFormat:@"%@",jsonDic[@"status"]];
             NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
             NSArray *details = [valueDict objectForKey:@"details"];

             NSLog(@"%@",details);
             for (NSDictionary *response in details){
                 NSLog(@"adsf %@",response);
             }
         }

Following is my error log

This can get the only 40404000024769 but not the value of 40404000024769. I tried using[response valueForKey:@"name"] and the app crashes. How can I be able to get the value of name, email?

2016-04-27 16:24:02.967 Vaighai Export[311:10625] ***Terminating app due to uncaught exception 'NSUnknownKeyException', reason:'[<__NSCFString 0x14e56870> valueForUndefinedKey:]:this class is not key value coding-compliant for the key name.'

Upvotes: 1

Views: 341

Answers (5)

NSNoob
NSNoob

Reputation: 5608

Your JSON structure is:

-NSDictionary
--Number
--NSDictionary
---NSDictionary
----NSDictionary
-----NSArray
------NSDictionary

The key details has a dictionary against it as value, not an array as you have assumed. Change your code to this:

NOTE: This is only sample to show you how you were parsing it wrong. You need to handle cases for your real world json in your app.

NSString *statusCode = [NSString stringWithFormat:@"%i",jsonDic[@"status"]]; //We got status
NSDictionary *valueDict = [jsonDic objectForKey:@"value"]; //We got value dic
NSDictionary *detailDic = [valueDict objectForKey:@"details"]; //We got details dic

NSArray * internalArr = [detailDic objectForKey:@"40404000024769"]; //We got array of dictionaries
 //Iterate over this array to log internal dictionaries
for(NSDictionary *nameDic in internalArr)
{
    NSLog(@"Name: %@ email: %@",[nameDic objectForKey:@"name"],[nameDic objectForKey:@"email"]);
}

Upvotes: 2

Himanshu padia
Himanshu padia

Reputation: 7712

key details is a dictionary not an array.

Just add one line in to fetch 40404000024769 key value.

Change your code as bellow:

NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];
NSArray *ar = [details objectForKey:@"40404000024769"];
for (NSDictionary *response in ar){
    NSLog(@"name: %@ email: %@",[response objectForKey:@"name"],[response objectForKey:@"email"]);
}

Upvotes: 0

vadian
vadian

Reputation: 285069

status is an integer

NSInteger statusCode = [jsonDic[@"status"] integerValue];

value contains a dictionary details

NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];

details contains an array in a dictionary. In item 0 of the array there is a dictionary containing the requested name and email keys.

for (NSString *key in details){
   NSLog(@"key %@",key);
   NSDictionary *data = details[key][0];
   NSLog(@"name %@ - email %@", data[@"name"], data[@"email"]);
}

Upvotes: 0

balkaran singh
balkaran singh

Reputation: 2786

use this code

 NSDictionary *valueDict = [jsonDic objectForKey:@"value"];

    NSDictionary *details = [valueDict objectForKey:@"details"];

    NSArray *YourArray= [details  objectForKey:@"40404000024769"];

    NSString *Name = [YourArray objectAtIndex:0]valueForKey:@"name"];
    NSString *Email = [YourArray objectAtIndex:0]valueForKey:@"email"];

Upvotes: 0

user5917312
user5917312

Reputation:

Firstly from the details dictionary fetch allkeys after that put that keys in the array then at zero index of that array the key is available find the array inside that key.

Upvotes: 0

Related Questions