Json parses local and online files (through interfaces)

Source: Internet
Author: User

Json parses local and online files (through interfaces)
Parse local files using json

NSString *path =[[NSBundle mainBundle] pathForResource:@“movielist” ofType:@“txt”];
// Specify the second parameter to specify a container to receive parsed data.
NSMutableDictionary *dic =[NSJSONSerialization JSONObjectWithData:data option:NSJSONReadingMutableContainers error:nil];NSMutableArray *movieArr =[NSMutableArray array];    for (NSMutableDictionary *temp in dic[@result]) {        Movie *movie =[[Movie alloc] init];        [movie setValuesForKeysWithDictionary:temp];        [movieArr addObject:movie];        [movie release];    }
Original code for parsing online files
NSString * strURL = @ http://api.map.baidu.com/place/v2/search? Query = Bank®Ion = Dalian & output = json & ak = 6e823f587c95f0148c%3539b99295;
A normal URL address cannot contain Chinese characters, but can only contain numbers, 26 uppercase/lowercase letters, and some special symbols, such as &, %, if you encounter a URL with Chinese characters, do not encode the original code first:
NSString *strEncode= [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Next, after the URL meets the requirements, start network requests. network requests are divided into three steps 1. Create an NSURL Based on the prepared URL
NSURL *url =[NSURL URLWithString:strEncode];
2. Send the original code of a request:
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
3. Return the data we want, an NSData object

Three parameters: the first parameter is the request just created, the second is the response returned, and the third is the error message.

Original code
 NSURLResponse *response = nil;    NSError *error =nil;    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Parse the returned data in json format.

Print all the bank names
Original code:

NSMutableDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];    for (NSDictionary *dic1 in dic[@results]) {        NSLog(@%@,dic1[@name]);    }

 

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.