使用AFNetworking解析JSON响应 [英] Parse JSON response with AFNetworking
本文介绍了使用AFNetworking解析JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Objective-C中使用 AFNetworking
设置了一个JSON帖子,并使用以下代码将数据发送到服务器:
I've setup a JSON post with AFNetworking
in Objective-C and am sending data to a server with the following code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"name": deviceName, @"model": modelName, @"pin": pin};
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"Content-Type" forHTTPHeaderField:@"application/json"];
[manager POST:@"SENSORED_OUT_URL" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
我通过相同的请求收到信息,并希望将数据发送到的NSString
。如何使用 AFNetworking
?
I'm receiving information through the same request, and want to send the data to a NSString
. How would I go about doing that with AFNetworking
?
推荐答案
responseObject
是NSArray或NSDictionary。您可以在运行时使用 isKindOfClass检查:
:
responseObject
is either an NSArray or NSDictionary. You can check at runtime using isKindOfClass:
:
if ([responseObject isKindOfClass:[NSArray class]]) {
NSArray *responseArray = responseObject;
/* do something with responseArray */
} else if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *responseDict = responseObject;
/* do something with responseDict */
}
如果你真的需要JSON的字符串,可通过查看 operation.responseString
获得。
If you really need the string of the JSON, it's available by looking at operation.responseString
.
这篇关于使用AFNetworking解析JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文