JSON parsing for local file parsing
NSString *path =[[NSBundle mainBundle] pathForResource:@“movielist” ofType:@“txt”];
The second parameter, develop a container to receive the 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]; }
Parsing the original code on the online file
*strURL =@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
A normal URL address is not allowed to have Chinese, can only have numbers, 26 letters of the case, and some special symbols, such as &,%, if encountered with the Chinese URL, first of all it does not encode the original code:
NSString *strEncode= [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Next, after the URL meets the requirements, the network request begins, and the network request is divided into three steps 1. Create a nsurl based on a URL that has been compiled
NSURL *url =[NSURL URLWithString:strEncode];
2. Send a request to the original code:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
3. Return the data we want, a NSData object
Three parameters: The first parameter is the request that was just created, the second is a response that is returned, and the third is the error message
Original code
NSURLResponse *response = nil; NSError *error =nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseerror:&error];
JSON parsing of data returned back
Print out all the bank names.
Original code:
NSMutableDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; for (NSDictionary *dic1 in dic[@"results"]) { NSLog(@"%@",dic1[@"name"]); }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JSON parsing of local and online files (via interface)