如何使用JSON格式解析使用AFNetworking 1.0获得的响应

时间:2015-03-07 17:22:11

标签: ios objective-c json post afnetworking

我遇到AFNetworking的问题。

目前,我可以POST格式[{1}}使用JSON向服务器发送AFJSONParameterEncoding请求并正确回复,问题是服务器回复了NSDictionary格式化的响应,响应我可以使用以下代码转换为JSON

NSString

问题是我无法转换任何其他格式的响应,而不是像之前发布的代码中的[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; // responseObject is the server response 那样。怎么可能呢?我想将响应转换为JSON格式,以便我可以读取一个精确的值,与键NSString

相关的值

到目前为止,这是我的代码:

"isInformative"

注意 - 我无法更新Xcode项目中捆绑的AFNetworking版本,因为它不是我自己的版本,所以遗憾的是我必须坚持并使用版本1.X

1 个答案:

答案 0 :(得分:1)

这解决了我的问题:

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:jailbreakRequest];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *requestOperation, id responseObject) {
    //Place this line of code below to create a NSDictionary from the async server response
    NSDictionary *jsonList = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    } failure:^(AFHTTPRequestOperation *requestOperation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[requestOperation start];

非常感谢: - )