IOS Stage Learning 22nd Day notes (JSON data format)

Source: Internet
Author: User

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)

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.