iOS 之征战json数据解析
/*
{
"code": "0",
"message": "suss",
"result": {
"rst": [
{
"id": "511359",
"title": "【中国新闻周刊】换换会更健康吗?还是跳槽只会带给你创伤",
"smalltitle": "图片来源:视觉中国",
"subhead": "",
"tags": "6302",
"taginfo": [
{
"id": "6302",
"name": "跳槽",
"type": "3"
}
],
*/
最外层是字典 字典里面又是字典 字典里面是数组 数组里面又是字典
- (void)addSetData
{
AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
// NSString *url = @"http://api.m.jiemian.com/cate/all.json";
NSString *url = @"http://api.m.jiemian.com/article/cate/117.json";
[manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
// NSLog(@"1221212122122%@", responseObject);
NSDictionary *dictionary = responseObject[@"result"];
NSArray *array = dictionary[@"rst"];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dic in array) {
NewsListModel *model = [[NewsListModel alloc]init];
[model setValuesForKeysWithDictionary:dic];
NSLog(@"%@", model.title);
[tempArray addObject:model];
}
} failure:^(NSURLSessionDataTask * task, NSError * error) {
NSLog(@"失败%@",error.debugDescription);
}];
}
第二种方式
{
"code": "0",
"message": "suss",
"result": [
{
"id": "1",
"name": "商业",
"show": "show_img_top",
"url": "http://api.m.jiemian.com/article/cate/117.json",
"img": "http://img.jiemian.com/101/original/20150515/143166967021642700.png",
"manage_img": "http://img.jiemian.com/101/original/20150521/143218683092607600.png",
"unistr": "117"
},
最外层是字典 字典里面是数组 数组里面是字典
- (void)addOther
{
AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
NSString *url = @"http://api.m.jiemian.com/cate/all.json";
[manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
// NSLog(@"%@", responseObject);
NSArray *array = responseObject[@"result"];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dic in array) {
NewsModel *model = [[NewsModel alloc]init];
[model setValuesForKeysWithDictionary:dic];
[tempArray addObject:model];
NSLog(@"%@", model.name);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"失败");
}];
}