-5

I have JSON DATA

{"respcode":0,"policyid":"1731958","Insuredid":"5625869"}

How can I get respcode, policyid and Insuredid in individual variable using parsing.

0

2 Answers 2

0

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"}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Bro, Work like a charm.
@DevNishad welcome :-)
0

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.

2 Comments

Why would you create a new dictionary if 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.
You are right. I edited my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.