Json data parsing and json Parsing

Source: Internet
Author: User

Json data parsing and json Parsing

Json file:

{    "count":"3",    "friend": [               {               "name": "zhangsan",               "pwd": "12345",               "nickName": "zs",               "sex": "nan"               },               {               "name": "lisi",               "pwd": "12345",               "nickName": "ls",               "sex": "nv"               },               {               "name": "wangwu",               "pwd": "12345",               "nickName": "ww",               "sex": "nan"               }             ]}

In a json file, {} is equivalent to a dictionary, and [] is equivalent to an array. Therefore, when parsing json data, different objects are created to receive data based on the obtained data.

First, store the data into data and use the system method NSJSONSerialization.

// System json parsing, // 1. the data object to be parsed. 2. the parsed NSMutableDictionary * datadic = [NSJSONSerialization JSONObjectWithData: data options: Unknown error: nil]; // obtain the NSString * count = [datadic objectForKey: @ "count"]; NSLog (@ "% @", count); // obtain the information array NSMutableArray * array = [datadic objectForKey: @ "friend"]; // print the output for (int I = 0; I <array. count; I ++) {NSMutableDictionary * dic = array [I]; NSLog (@ "% @", [dic objectForKey: @ "name"]); NSLog (@ "% @", [dic objectForKey: @ "pwd"]); NSLog (@ "% @", [dic objectForKey: @ "nickName"]); NSLog (@ "% @", [dic objectForKey: @ "sex"]);}

The options parameter specifies the JSON parsing mode. Three formats are available.

 

NSJSONReadingMutableContainers. Specifies a variable array or dictionary returned by parsing. If you need to modify the results in the future, it is appropriate for the Turing community member FAID to exclusively respect copyright.
NSJSONReadingMutableLeaves. The specified leaf node is a variable string.
NSJSONReadingAllowFragments. The specified top-level node can be an array or dictionary.

 

 

 

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.