简体   繁体   中英

AFNetworking alters JSON response?

I want to use AFNetworking for communicating to a RESTful webservice. I get a JSON response, but for some reason it is different from what the webservice sent.

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

But for some reason println prints this:

{
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 , ...).

ie AFNetworking has already done a bunch of work for you to deserialise the data that was received.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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