iOS Swift: Parsing response json with AFNetworking
Asked Answered
M

3

10

So, I am using AFNetworking 2.0 (ObjC framework with Bridging-Header) to make some requests on a local server. I have followed a few tutorials to code it using Swift. This is the code:

var success = { (operation:AFHTTPRequestOperation!, response:AnyObject!) -> Void in
    println(response.description)
    successBlock(result:response.description)
}

var failure = { (operation:AFHTTPRequestOperation!, response:NSError!) -> Void in
    println(response.description)
    errorBlock(error:response.description)
}

var manager = AFHTTPRequestOperationManager()
manager.responseSerializer = AFJSONResponseSerializer();
manager.GET("http://127.0.0.1:8080/api/manufacturer", parameters: nil, success: success, failure: failure)

It retrieves the json and prints it successfully. The response is something like this:

(
        {
        "_id" = 539f0973e3c7f4ab1f6078f5;
        name = Manufacturer01;
    },
        {
        "_id" = 539f18c5e3c7f4ab1f6078f6;
        name = Manufacturer02;
    }
)

However, I am unable to parse it... I tried response[0] to get the first element, but it crashes the simulator and even Xcode6 when I try to do: (lldb) > po response[0]. I tried everything, every example I have seen explains how to print the result but nothing about parsing each field.

The response object looks like this when I try to debug it:

value = Some {
    Some = (instance_type = Builtin.RawPointer = 0x0b240710 -> 0x00bc5da0 (void *)0x00bc5db4: __NSCFArray)
  }

Any clue? Thanks in advance!

Marbles answered 16/6, 2014 at 16:36 Comment(5)
I also have this issue. How on Earth do i assign the JSON Dictionary values to a string variable? I have a var var serverID:String? and when trying self.serverID = responseObject["server_id"] as? String it crashes XcodeMohsen
I had success with this approach: #24260256Roughhew
@Roughhew Unfortunately I get EXC_BAD_INSTRUCTION on Simulator and EXC_BREAKPOINT on Device with that code. :(Marbles
@Mohsen It seems automatically convert to NUMBER TYPE, not String!!! I had same issue, and I try to find a way to convert as String.Kokanee
hi any solution for this?Judithjuditha
C
1

try this

if let responseArray = response as? NSArray {
    let firstElement = responseArray[0]
    // do something with the first element
}
Cornetcy answered 24/10, 2015 at 17:15 Comment(0)
C
0

I think your problem lays within sending it back in the successBlock. Since the retrieved information ain't presented properly in the description object.

var jsonArrayDictionary = response.result.value as? [[String: Any]]

for item in jsonArrayDictionary {
     dump(item["_id"] as? String)
     dump(item["name"] as? String)
}

That should probably do it.

Coursing answered 10/5, 2017 at 12:20 Comment(0)
S
-5

Your example response is not valid JSON.

If your example is an Array, JSON would have square brackets instead of parens, field names in quotes and colons instead of equals signs. For example:

[
  {
    "_id": 1234,
    "name": "foo bar"
  },
  {
    "_id": 12122,
    "name": "baz"
  }
]

Also see one of the JSON linters, e.g.: jsonlint.com

Sandor answered 16/6, 2014 at 17:11 Comment(2)
It's probably the contents of the dictionary printed in the debugger.Pontias
@Pontias is right. The json is well constructed, that is the debugger representation.Marbles

© 2022 - 2024 — McMap. All rights reserved.