使用 AFNetworking 和模型类解析 JSON
Posted
技术标签:
【中文标题】使用 AFNetworking 和模型类解析 JSON【英文标题】:JSON parsing with AFNetworking and model Class 【发布时间】:2015-10-05 05:05:34 【问题描述】:我在使用模型从 json 获取数据并将数据显示到我的应用程序时有点困惑。
我有这种 json 数据:
result
[
key : value
key : value
key : value
key : value
key : value
key : value
]
我正在尝试这种代码:
json = [[NSMutableArray alloc]init];
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:&writeError];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&writeError];
json = dic[@"Result"];
int i;
for (i = 0; i <= json.count; i++)
NSMutableArray *array = json[i];
[json enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
// Dim = [[DimEntityFull alloc]initWithJSONDictionary:obj];
saveSearch = [[SaveSearchMaster alloc]initWithJSONDictionary:obj];
];
我正在使用“AFNetworking”,我正在尝试获取数据并存储到模型类中,然后显示到自定义单元格标签。
我怎样才能得到它。
谢谢。
【问题讨论】:
您是否尝试过在提取的每个阶段记录数据? 是的,但它对我不起作用。我对如何存储到模型类有点困惑。 我知道 arrayOfRecords = [jsonData objectForKey:@"result"];然后 cell.lblName.text = [[arrayOfRecords objectAtIndex:indexPath.row]valueForKey:@"name"];是正确的流程。但是使用模型类我怎样才能得到相同的结果? 【参考方案1】:在您的视图控制器中
- (void)viewDidLoad
[super viewDidLoad];
[self getUsersList];
-(void)getUsersList
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
[manager POST:[NSString stringWithFormat:@"http://www.yourdomainname.com/getUserList"] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
//we will add Modal class objects of users in this array
usersArray=[[NSMutableArray alloc]init];
//getting result dictionary from response
NSDictionary *resultDictinary = [responseObject objectForKey:@"result"];
for (NSDictionary *userDictionary in resultDictinary)
//allocating new user from the dictionary
User *newUSer=[[User alloc]initWithDictionary:userDictionary];
[usersArray addObject:newUSer];
//in users array, you have objects of User class
//reload your tableview data
[self.TableView reloadData];
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
现在创建名为“用户”的新文件
在User.h
@interface User : NSObject
NSString *userID;
NSString *firstName;
NSString *lastName;
@property (nonatomic, retain)NSString *userID;
@property (nonatomic, retain)NSString *firstName;
@property (nonatomic, retain)NSString *lastName;
-(id)initWithDictionary:(NSDictionary *)sourceDictionary;
@end
在User.m
#import "用户.h"
@implementation User
@synthesize userID,firstName,lastName;
-(id)initWithDictionary:(NSDictionary *)sourceDictionary
self = [super init];
if (self != nil)
self.firstName=[sourceDictionary valueForKey:@"firstName"];
self.lastName=[sourceDictionary valueForKey:@"lastName"];
self.userID=[sourceDictionary valueForKey:@"userID"];
return self;
@end
在您的numberOfRowsInSection
方法中return usersArray.count;
在您的 cellForRowAtIndexPath
方法中
User *user=(User *)[usersArray objectAtIndex:indexPath.row];
yourLabel.text=user.firstName;
【讨论】:
以上是关于使用 AFNetworking 和模型类解析 JSON的主要内容,如果未能解决你的问题,请参考以下文章
iOS网络——AFNetworking AFURLSessionManager源码解析
iOS网络——AFNetworking AFURLSessionManager源码解析
iOS网络——AFNetworking AFHttpSessionManager源码解析
iOS网络——AFNetworking AFHttpSessionManager源码解析