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.