从帖子请求ios afnetworking解析json

时间:2014-02-21 01:51:41

标签: ios objective-c json afnetworking

我正在开发一个iOS应用程序(对iOS来说很新)我的注册流工作在哪里我将数据发布到我的服务器...服务器返回JSON,但我的问题是,如何将其解析为变量?

它返回的数据是:

 {"token":"67f41f60-9a9a-11e3-8310-11b6baf18c40","userId":13,"stationId":1}

我用来发布帖子请求的代码是:

 [manager POST:@"http://localhost:3000/service/register_user/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error!!!!!: %@", error);
}];

另外,当我发布帖子请求时,这就是我在xcode中登录的内容

 2014-02-20 20:50:39.799 FlashoverResponse[4668:70b] Error!!!!!: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8a5e4f0 {NSErrorFailingURLKey=http://localhost:3000/service/register_user/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a5e200> { URL: http://localhost:3000/service/register_user/ } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 74;
"Content-Type" = "text/html; charset=utf-8";
Date = "Fri, 21 Feb 2014 01:50:39 GMT";
"X-Powered-By" = Express;

}},NSLocalizedDescription =请求失败:不可接受的内容类型:text / html}

1 个答案:

答案 0 :(得分:4)

您的服务器是否已配置为在text/json application/json

中进行回复

和/或你设置了吗?

manager.responseSerializer.acceptableContentTypes

此解析器将尝试解析的内容类型为NSSet,如果您无法更改服务器以在json中进行响应,则可以将text/html添加到该集合中,因此解析器无论如何都会解析它。