IOS growth path-JSON Parsing

Source: Internet
Author: User

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...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.