Json总结
Json格式
基本格式
变量名:值
"title":"肖申克的救赎"
值可以是结构体(类),如下,用大括号括起来:
"images": {"small":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/ipst\/public\/p480747492.jpg", "large":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/lpst\/public\/p480747492.jpg", "medium":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/spst\/public\/p480747492.jpg"},
值可以是数组,如下,用中括号括起来:
"genres":["犯罪","剧情"],
数组可以由结构体(类)构成:
"casts":[ {"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/17525.jpg","large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/17525.jpg","medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/17525.jpg"}, "alt":"http:\/\/movie.douban.com\/celebrity\/1054521\/“,”id":"1054521", "name":"蒂姆·罗宾斯"}, {"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/34642.jpg","large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/34642.jpg","medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/34642.jpg"}, "alt":"http:\/\/movie.douban.com\/celebrity\/1054534\/", "id":"1054534", "name":"摩根·弗里曼”}, {“avatars":{"small":"http:\/\/img5.douban.com\/img\/celebrity\/sL;POL,mall\/5837.jpg","large":"http:\/\/img5.douban.com\/img\/celebrity\/large\/5837.jpg","medium":"http:\/\/img5.douban.com\/img\/celebrity\/medium\/5837.jpg"}, "alt":"http:\/\/movie.douban.com\/celebrity\/1041179\/", "id":"1041179", "name":"鲍勃·冈顿"}],
举例
http://api.douban.com/v2/movie/top250
{"rating":{"max":10,"average":9.6,"stars":"50","min":0}, "genres":["犯罪","剧情"], "collect_count":783892, "casts":[ {"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/17525.jpg","large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/17525.jpg","medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/17525.jpg"}, "alt":"http:\/\/movie.douban.com\/celebrity\/1054521\/“,”id":"1054521", "name":"蒂姆·罗宾斯"}, {"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/34642.jpg","large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/34642.jpg","medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/34642.jpg"}, "alt":"http:\/\/movie.douban.com\/celebrity\/1054534\/", "id":"1054534", "name":"摩根·弗里曼”}, {“avatars":{"small":"http:\/\/img5.douban.com\/img\/celebrity\/sL;POL,mall\/5837.jpg","large":"http:\/\/img5.douban.com\/img\/celebrity\/large\/5837.jpg","medium":"http:\/\/img5.douban.com\/img\/celebrity\/medium\/5837.jpg"}, "alt":"http:\/\/movie.douban.com\/celebrity\/1041179\/", "id":"1041179", "name":"鲍勃·冈顿"}], "title":"肖申克的救赎", "original_title":"The Shawshank Redemption", "subtype":"movie", "directors": [{"avatars": {"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/230.jpg","large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/230.jpg","medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/230.jpg"}, "alt":"http:\/\/movie.douban.com\/celebrity\/1047973\/", "id":"1047973", "name":"弗兰克·德拉邦特"}], "year":"1994", "images": {"small":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/ipst\/public\/p480747492.jpg", "large":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/lpst\/public\/p480747492.jpg", "medium":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/spst\/public\/p480747492.jpg"}, "alt":"http:\/\/movie.douban.com\/subject\/1292052\/", "id":"1292052"},
NSJSONSerialization——IOS5自带
JSONObjectWithData创建json对象
#pragma mark - ASIHTTPRequestDelegate - (void)requestFinished:(ASIHTTPRequest *)request { NSError *error; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:request.responseData options:kNilOptions error:&error]; NSArray *movieArray = [jsonDict objectForKey:@"subjects"]; // NSDictionary *jsonDict = [request.responseString JSONValue]; // NSArray *movieArray = jsonDict[@"subjects"]; for (NSDictionary *dict in movieArray) { SYMovieModel *model = [[SYMovieModel alloc] init]; model.movieName = dict[@"title"]; model.movieYear = dict[@"year"]; model.movieImage = dict[@"images"][@"large"]; [_saveDataArray addObject:model]; } //刷新表 [_tableView reloadData]; }
获取webView中的json
- (void)webViewDidFinishLoad:(UIWebView *)webView{ NSString *jsonStr = [self.webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]; NSString *cmd = [NSString stringWithFormat:@"eval(%@).userId", jsonStr]; NSString *str = [self.webView stringByEvaluatingJavaScriptFromString:cmd]; cmd = [NSString stringWithFormat:@"eval(%@).result", jsonStr]; str = [self.webView stringByEvaluatingJavaScriptFromString:cmd]; if ([str isEqualToString:@"Success"]){ NSLog(@"Success"); [self dismissViewControllerAnimated:NO completion:^{}]; } }
其他方法