简体   繁体   English

从发布请求ios解析afsoning解析json

[英]parse json from post request ios afnetworking

I'm working on an iOS app (pretty new to iOS) and I have my registration flow working where i post data to my server... The server returns JSON, but my question is, how do I parse that into variables? 我正在使用iOS应用程序(iOS的新手),我的注册流程在向服务器发布数据的地方工作...服务器返回JSON,但我的问题是,如何将其解析为变量?

The data that it returns is: 它返回的数据是:

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

The code I'm using to make the post request is: 我用来发出发布请求的代码是:

 [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);
}];

Also, when I make the post request, this is what I get in the log in xcode 另外,当我发出发布请求时,这就是我在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=Request failed: unacceptable content-type: text/html} }},NSLocalizedDescription =请求失败:不可接受的内容类型:text / html}

Is your server configured to respond in text/json application/json ? 您的服务器是否配置为在text/json application/json响应?

and/or have you set this? 和/或您设置了这个吗?

manager.responseSerializer.acceptableContentTypes

it takes a NSSet of content types this parser will attempt to parse, if you cannot change the server to respond in json you can add text/html into that set, so the parser will parse it anyway. 它需要此解析器将尝试解析的内容类型的NSSet ,如果无法更改服务器以json响应,则可以将text/html添加到该集合中,因此解析器仍将对其进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM