使用AFNetworking和model进行JSON解析类
问题描述:
我在使用Model获取数据和将数据从json显示到我的应用程序时感到有点困惑。使用AFNetworking和model进行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” 和我试图获取数据并存储到模型类中,然后显示为自定义单元标签。
我怎样才能得到它。
谢谢。
答
在您的视图控制器
- (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.m
#进口 “User.h” 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
@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;
您是否尝试过在提取的每个阶段记录的数据? – ZeMoon
是的,但它不适合我。我对如何存储模型类感到困惑。 –
我知道arrayOfRecords = [jsonData objectForKey:@“result”];然后cell.lblName.text = [[arrayOfRecords objectAtIndex:indexPath.row] valueForKey:@“name”];是正确的流程。但与模型类我怎么能得到相同的结果? –