JSON 使用 AFNetworking 和模型解析 Class
JSON parsing with AFNetworking and model Class
我在使用模型从 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",我正在尝试获取数据并存储到模型 class 中,然后显示到自定义单元格标签。
如何获取。
谢谢。
在你的视图控制器中
- (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'
的新文件
在 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 "User.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;
我在使用模型从 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",我正在尝试获取数据并存储到模型 class 中,然后显示到自定义单元格标签。
如何获取。
谢谢。
在你的视图控制器中
- (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'
的新文件
在 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 "User.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;