简体   繁体   English

AFNetworking会更改JSON响应吗?

[英]AFNetworking alters JSON response?

I want to use AFNetworking for communicating to a RESTful webservice. 我想使用AFNetworking与RESTful Web服务进行通信。 I get a JSON response, but for some reason it is different from what the webservice sent. 我收到JSON响应,但由于某种原因,它与Web服务发送的内容不同。

My code: 我的代码:

let manager = AFHTTPRequestOperationManager()
var contentTypes:NSMutableSet = manager.responseSerializer.acceptableContentTypes.mutableCopy() as NSMutableSet
contentTypes.addObject("text/html")
manager.responseSerializer.acceptableContentTypes = contentTypes.copy() as NSSet
var parameters = ["format":"json"]
manager.GET( "http://www.raywenderlich.com/demos/weather_sample/weather.php",
parameters: parameters,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
             println("JSON: " + responseObject.description as String)
       },
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
            println("Error: " + error.localizedDescription)
        })

The JSON should look like this: http://www.raywenderlich.com/demos/weather_sample/weather.php?format=json JSON应该看起来像这样: http ://www.raywenderlich.com/demos/weather_sample/weather.php?format =json

But for some reason println prints this: 但是出于某种原因,println会输出以下内容:

{
data =     {
    "current_condition" =         (
                    {
            cloudcover = 16;
            humidity = 59;
            "observation_time" = "09:09 PM"; 
...

The : are replaced with =, [] with () and most of " are gone. :替换为=,[]替换为(),大部分“都消失了。

Does anybody know the reason for this? 有人知道原因吗?

Because it isn't printing JSON, it's printing the object graph that was generated from the JSON (a combination of instances of NSArray , NSDictionary , NSString , ...). 因为它不打印JSON,所以它打印从JSON生成的对象图( NSArrayNSDictionaryNSString …的实例的组合)。

ie AFNetworking has already done a bunch of work for you to deserialise the data that was received. 即AFNetworking已经为您进行了大量的工作来反序列化接收到的数据。

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

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