我有JSON DATA
{ “respcode”:0, “策略ID”: “1731958”, “Insuredid”: “5625869”}
如何使用解析在单个变量中获取respcode,policyid和Insuredid。
答案 0 :(得分:0)
已修改如果您的JSON数据已经是字典,您可以使用以下方式访问它们:
NSLog(@"%@",jsonData[@"respcode"]); // 0
NSLog(@"%@",jsonData[@"policyid"]); // 1731958
NSLog(@"%@",jsonData[@"Insuredid"]); // 5625869
我希望这可以帮到你。如果没有,请告诉我有关您的代码的更多信息,以便我可以尝试更好的答案。
答案 1 :(得分:0)
试用以下代码:
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"}