Json data:
{"Result": [{"meeting": {"addr": "203", "creator": "Zhang Yi", "member": [{"name ": "Zhang 'er", "age": "20" },{ "name": "Zhang San", "age": "21" },{ "name": "Zhang Si ", "age": "22"}] }}, {"meeting": {"addr": "204", "creator": "Zhang 'er", "member ": [{"name": "Zhang 'er", "age": "20" },{ "name": "Zhang San", "age": "21 "}, {"name": "Zhang Si", "age": "22"}]}
1. Obtain the json file path and obtain the data in the path:
NSString *path=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"]; NSString *_jsonContent=[[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
2. Obtain the key-value pairs based on the obtained _ jsonContent String object:
NSMutableDictionary dict = [_ jsonContent JSONValue]; // you can use the JSONValue defined by the system instead of defining the obtained method.
3. JSON parsing Based on the obtained key-value pairs:
/* According to the logical relationship between the preceding json data, we can know the resolution order: 1. obtain the key-value pairs based on the obtained string. obtain the corresponding value based on the obtained key-value pair, that is, the array in the value, 3. then obtain the key-value pairs in the array, 4. then, based on the obtained key-value pair, obtain the value in the key-value pair through the key * // json resolution // 2. NSArray * result = [_ dict objectForKey: @ "result"]; // 3. NSDictionary * dic = [result objectAtIndex: 0]; // 4. NSDictionary * meeting = [dic objectForKey: @ "meeting"]; // obtain the addr value NSString * address = [meeting objectForKey: @ "addr"]; // obtain the creator value NSString * creator = [meeting objectForKey: @ "creator"]; // obtain the data in member because the key value contains an array, so repeat the above 2, 3, 4 actions // 2. NSArray * members = [meeting objectForKey: @ "member"]; // 3. // here the for loop statement is used, for (NSDictionary * member in members) {// 4. NSString * name = [member objectForKey: @ "name"]; NSString * age = [member objectForKey: @ "age"];}
In this way, json data can be parsed...