IOS stage learning 22nd-day notes (JSON data format Introduction), 22nd-day json

Source: Internet
Author: User

IOS stage learning 22nd-day notes (JSON data format Introduction), 22nd-day json

Knowledge points of IOS Learning (oc language)

I. JSON Data Format

1) concept: json is A network data transmission format with A value/object: {"A": 1, "B": "2 "...} Dictionary; Object Sequence: [,] Two Data Types

 

2) URLWithString encapsulates the string URL into an NSURL object, for example:

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 encapsulates the local file address into an NSURL object, for example:

1 url=[NSURL fileURLWithPath:@“Users/kingkong/JsonFile/test.json”];

 

4) initWithContentsOfURL is used to synchronize json data on the request network, for example:

1 NSData *json=[[NSData alloc]initWithContentsOfURL:url];

5) initWithData parses JSON data into a string, for example:

1 NSString *strjson=[[NSString alloc]initWithData:json encoding:NSUTF8StringEncoding];2 NSLog(@"%@",strjson);

 

6) options: NSJSONReadingAllowFragments can directly parse json data into dictionary objects, for example:

1 // read the file content (json data) 2 NSData * jsonData = [[NSData alloc] initWithContentsOfFile: path]; 3 // parse json data directly to the dictionary object 4 NSDictionary * dict1 = [NSJSONSerialization JSONObjectWithData: jsonData options: 5 NSJSONReadingAllowFragments error: nil];

 

7) obtain the network image data and save it to a local (similar to download) instance code

1 // obtain resources (image data) on the server 2 NSData * iconData = [NSData dataWithContentsOfURL: [NSURL URLWithString: iconUrl]; 3 NSString * iconFile = [fullPath stringByAppendingPathComponent: @ "icon.png"]; 4 // write image data to a file (Save the image to the file) 5 [iconData writeToFile: iconFile atomically: YES];

 

8) Obtain JSON data and traverse the data instance code:

1 // request network path 2 NSString * path = @ "http: // 10.0.8.8/sns/my/user_list.php? Number = 20 & page = "; 3 // construct URL 4 NSURL * url = [NSURL URLWithString: path]; 5 // request to obtain JSON data 6 NSData * json = [[NSData alloc] initWithContentsOfURL: url]; 7 // parse JSON data into object 8 id obj = [NSJSONSerialization JSONObjectWithData: json options: NSJSONReadingMutableContainers error: nil]; 9 // traverse JSON data 10 if ([obj isKindOfClass: [NSDictionary class]) {11 NSDictionary * dict = (NSDictionary *) obj; 12 NSArray * array = [dict objectForKey: @ "users"]; 13 for (NSDictionary * dic in array) {14 NSLog (@ "username: % @ \ tuid: % @ ", [dic objectForKey: @" username "], [dic objectForKey: @" uid "]); 15} 16}

9) encode the dictionary set into JSON data instance code

1 // construct Dictionary data 2 NSArray * arry = @ [@ "pass1234", @ "123456"]; 3 NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys: @ "KingKong", @ "username", @ "male", @ "sex", arry, @ "password", nil]; 4 // convert dictionary set data to JSON data type 5 NSData * json = [NSJSONSerialization dataWithJSONObject: dic options: NSJSONWritingPrettyPrinted error: nil]; 6 // reparse JSON data 7 NSString * strjson = [[NSString alloc] initWithData: json encoding: NSUTF8StringEncoding]; 8 NSLog (@ "% @", strjson );

10) JSON parsing tool Jason. app [Download]

11) Date function operations in NSDate OC [Details]

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.