1 //JSON2 //a subset of JavaScript syntax3 //Key:value similar to dictionary objects4 //JSON is composed of arrays and objects5 6 //The outermost structure of the 1.json file is a dictionary or an array, most of which are dictionaries7 //2.json data can be a basic data type, a string object, an Array object ([] contains), a Dictionary object, a logical object ({} contains), NULL,8 9 intMainintargcConst Char*argv[]) {Ten @autoreleasepool { One A #if0 - //1. First read the JSON file to the bit NSData type data - //NSData *data = [NSData datawithcontentsoffile:@ ""]; theNSString *datastring = [NSString stringwithcontentsoffile:@"/users/qianfeng/desktop/day21-oc11_ Time Class/day21_oc11_json file parsing/network JSON parsing. txt"encoding:nsutf8stringencoding Error:nil]; -NSData *data =[datastring datausingencoding:nsutf8stringencoding]; - - //2. Parsing JSON data +Nsdictionary *dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; - +NSLog (@"%@", dict); A #endif at } - - @autoreleasepool { - //parsing JSON URLs - //Convert a String object to a URL -NSString *aurl =@"Http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_ key=%e9%93%b6%e9%ad%82&bk_length=600"; inNsurl *url =[[Nsurl alloc] initwithstring:aurl]; -NSString *urlstring =[NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding Error:nil]; to +NSData *data =[URLString datausingencoding:nsutf8stringencoding]; -Nsdictionary *dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; the *NSLog (@"%@", dict); $ }Panax Notoginseng return 0; -}
Oc-json file Parsing-json web site Learning