OGeek|极客世界-中国程序员成长平台

标题: ios - 从发布请求 ios afnetworking 解析 json [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:34
标题: ios - 从发布请求 ios afnetworking 解析 json

我正在开发一个 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);
}];

另外,当我发出 post 请求时,这是我在 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=请求失败: Not Acceptable 内容类型:text/html}



Best Answer-推荐答案


您的服务器是否配置为在 text/json application/json 中响应?

和/或你设置了这个?

manager.responseSerializer.acceptableContentTypes

它需要一个 NSSet 这个解析器将尝试解析的内容类型,如果你不能改变服务器以响应 json 你可以添加 text/html 到那个集合中,所以解析器无论如何都会解析它。

关于ios - 从发布请求 ios afnetworking 解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923644/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4