在 iOS 5 中使用 AFNetworking 解析 JSON
Posted
技术标签:
【中文标题】在 iOS 5 中使用 AFNetworking 解析 JSON【英文标题】:Parsing JSON in iOS 5 with AFNetworking 【发布时间】:2013-02-27 08:47:16 【问题描述】:我想用 ios 5 中的 AFNetworking 框架解析从服务器接收到的 JSON 数据:
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
failure:nil];
我要解析的 JSON 数据如下所示:
[
addressReal = "\U6912\U6c5f\U89e3\U653e\U5357\U8def295\U53f7";
areaTotal = 774;
balance = 0;
businessIncome = "556.49";
equNoString = "";
equStatusSting = "";
id = 208;
staff = 10;
,
addressReal = "\U53f0\U5dde\U5e02\U5e9c\U5927\U9053668\U53f7";
areaTotal = 156;
balance = 6463;
businessIncome = "174.93";
equStatusSting = "";
id = 209;
staff = 8;
]
我现在如何访问 JSON?
【问题讨论】:
我试图清理您的问题并使其真正可以回答,但我建议您尝试确保您始终清楚地说明您需要帮助解决的问题。另请参阅How To s The Smart Way(特别是:准确并提供有关您的问题的信息) 【参考方案1】:这是我使用的,请随意满足您的需求:
-(void) loginWithUserID:(NSString*)usrID User:(NSString*)usr Password:(NSString *)psw Sender:(id)sender
if ([sender respondsToSelector:@selector(startLoading)])
[sender performSelector:@selector(startLoading)];
baseURL = [NSString stringWithFormat:
@"http://xxx.xxx.xxx.xxx/test/service.svc/weblogin"];
NSString* serviceURL = [NSString stringWithFormat:
@"%@?userID=%@&user=%@&password=%@",baseURL,usrID,usr,psw;
NSURL *url = [NSURL URLWithString:[serviceURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
self.LoginOK = [JSON valueForKeyPath:@"LoginOK"];
if (LoginOK.intValue == 1)
NSDictionary *usrData = [JSON valueForKeyPath:@"userData"];
userData = [[UserData alloc]init];
[userData readFromJSONDictionary:usrData];
NSLog(@"Userdata: %@",userData);
if ([sender respondsToSelector:@selector(loginSuccessful:)])
[sender performSelector:@selector(loginSuccessful:)];
else
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No Correct Login Credentials" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertView show];
;
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
if ([sender respondsToSelector:@selector(stopLoading)])
[sender performSelector:@selector(stopLoading)];
[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"Loginservice not available! Check your connection!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil] show];
NSLog(@"Error: %@", error);
];
[operation start];
;
并使用一个
-(void)readFromJSONDictionary:(NSDictionary *)d
[self setProperty1:[d objectForKey:@"property1"]];
将该字典中的所有属性获取到自定义类中的方法。
所以在你的情况下它会使用
NSDictionary *data = [JSON valueForKeyPath:@"data"];
【讨论】:
嗯,是的,这是一个示例,以防 JSON 出现在名为 data 的容器中。如果此答案对您有所帮助,并且您认为这是您问题的答案,请您按一下旁边的复选标记好吗?如果它没有回答您的问题,或者您需要更多说明,请不要犹豫!以上是关于在 iOS 5 中使用 AFNetworking 解析 JSON的主要内容,如果未能解决你的问题,请参考以下文章
无法在 ios 中使用 afnetworking 发布 JSON
如何使用 Afnetworking 在 ios 中在后台上传图像