I have JSON DATA
{"respcode":0,"policyid":"1731958","Insuredid":"5625869"}
How can I get respcode, policyid and Insuredid in individual variable using parsing.
Try out the below code:
NSString* path = [[NSBundle mainBundle] pathForResource:@"JSON" ofType:@"json"];
NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *responseDictionary = [NSJSONSerialization
JSONObjectWithData:jsonData
options:0
error:&error];
if(! error) {
NSString *insuredId = [responseDictionary objectForKey:@"Insuredid"];
NSString *policyid = [responseDictionary objectForKey:@"policyid"];
NSString *respcode = [responseDictionary objectForKey:@"respcode"];
NSLog(@"insuredId : %@ & policyid : %@ & respcode : %@",insuredId,policyid,respcode);
} else {
NSLog(@"Error in parsing JSON");
}
//JSON.json
{"respcode":0,"policyid":"1731958","Insuredid":"5625869"}
Edited If your JSON DATA is already a Dictionary, you can access them using:
NSLog(@"%@",jsonData[@"respcode"]); // 0
NSLog(@"%@",jsonData[@"policyid"]); // 1731958
NSLog(@"%@",jsonData[@"Insuredid"]); // 5625869
I hope this can help you. If not, let me know more information about your code, so I can try a better answer.
responseObject is already a dictionary? And what is responseObject? Your answer is leaving out a lot of relevant details. You don't even know what the OP has. They may only have a string. They may have already parsed the JSON into a dictionary but don't know how to get the values from the dictionary.