iOS Learning (OC language) Knowledge Point finishing
One, JSON data format
1) Concept: JSON is a network data transfer format with values/objects: {"A": 1, "B": "2" ...} A sequence of objects: [,,,,,] array of two data types
2) urlwithstring the string URLs into Nsurl objects such as:
1 nsstring *urlstr=@ "http://10.0.8.8/sns/my/user_list.php?number=202 &page="; // get Post3 nsurl *url=[nsurl urlwithstring:urlstr];
3) Fileurlwithpath to encapsulate local files into Nsurl objects such as:
1 url=[nsurl fileurlwithpath:@ "Users/kingkong/jsonfile/test.json"];
4) Initwithcontentsofurl is used to synchronize JSON data on the request network such as:
1 nsdata *json=[[nsdata Alloc]initwithcontentsofurl:url];
5) Initwithdata parse the JSON data into strings such as:
1 nsstring *strjson=[[NSString Alloc]initwithdata:json encoding:nsutf8stringencoding]; 2 NSLog (@ "%@", Strjson);
6) Options:nsjsonreadingallowfragments can parse JSON data directly into a Dictionary object such as :
1 // Read file contents (JSON-formatted data) 2 nsdata *jsondata=[[NSData Alloc]initwithcontentsoffile:path]; 3 // parse JSON data directly into a Dictionary object 4 nsdictionary *dict1=[nsjsonserialization jsonobjectwithdata:jsondata options:5 Nsjsonreadingallowfragments Error:nil];
7) Get network picture data and save to local (similar to download) instance code
1 // get resources on the server (picture data)2 nsdata *icondata=[NSData datawithcontentsofurl:[nsurl Urlwithstring:iconurl ]]; 3 nsstring *iconfile=[fullpath stringbyappendingpathcomponent:@ "icon.png" ]; 4 // write picture data to a file (save picture to file) 5 [Icondata Writetofile:iconfile Atomically:yes];
8) Get the JSON data and traverse the data instance code:
1 //the requested network path2NSString *path=@"http://10.0.8.8/sns/my/user_list.php?number=20&page="; 3 //construct URL4Nsurl *url =[Nsurl Urlwithstring:path];5 //Request for JSON data6NSData *json=[[NSData Alloc]initwithcontentsofurl:url]; 7 //parsing JSON data into objects8 IDobj=[nsjsonserialization Jsonobjectwithdata:json options:nsjsonreadingmutablecontainers Error:nil];9 //traversing JSON dataTen if([obj Iskindofclass:[nsdictionaryclass]]) { OneNsdictionary *dict= (Nsdictionary *) obj; ANsarray *array=[dict Objectforkey:@"Users"]; - for(Nsdictionary *dicinchArray) { -NSLog (@"username:%@\tuid:%@", [dic Objectforkey:@"username"],[dic Objectforkey:@"UID"]); the } -}
9) encode the dictionary collection into the JSON data instance code
1 //Constructing dictionary Data2Nsarray *[email protected][@"pass1234",@"123456" ];3Nsdictionary *dic=[[nsdictionary Alloc]initwithobjectsandkeys:@"KingKong",@"username",@"male",@"Sex", Arry,@"Password", nil];4 //Convert a dictionary collection data to a JSON data type5NSData *json=[nsjsonserialization datawithjsonobject:dic options:nsjsonwritingprettyprinted Error:nil];6 //re-parsing JSON data7NSString *strjson=[[NSString Alloc]initwithdata:json encoding:nsutf8stringencoding];8NSLog (@"%@", Strjson);
JSON parsing tool jason.app "Download"
NSDate oc in the date function operation "Details"
IOS Stage Learning 22nd Day notes (JSON data format)