Alamofire returns wrong Json format

Hi,

I'm retrieving a json by using this piece of code:

AF.request("https://postman-echo.com/get?foo1=bar1&foo2=bar2").responseJSON { response in
                print(response)
            }


Problem is that I get this:

{
    args = {
        foo1 = bar1;
        foo2 = bar2;
    };


Instead of this:

{ 
   "args":{ 
      "foo1":"bar1",
      "foo2":"bar2"
   },

Why? Shouldn't it return a properly formatted json like in the second example?

Accepted Reply

Shouldn't it return a properly formatted json like in the second example?

No. Your saying-properly formatted json is sort of an intermediate form, which is lost when read into Swift.


Once read into Swift, all JSON items are converted as follows:

JSON null - NSNull

JSON true/false - NSNumber

JSON number - NSNumber

JSON string - NSString

JSON array - NSArray

JSON object - NSDictionary


This output:

{

args = {

foo1 = bar1;

foo2 = bar2;

};

means that it is an NSDictionary containing a single entry with key: "args" and value: another NSDictionary.

So, you have successfully read the JSON into Swift.


If you want to see the intermediate form, you may need to access the data reprsenting the response body, and convert it to text,

which you do not need other than for debugging.

Replies

Shouldn't it return a properly formatted json like in the second example?

No. Your saying-properly formatted json is sort of an intermediate form, which is lost when read into Swift.


Once read into Swift, all JSON items are converted as follows:

JSON null - NSNull

JSON true/false - NSNumber

JSON number - NSNumber

JSON string - NSString

JSON array - NSArray

JSON object - NSDictionary


This output:

{

args = {

foo1 = bar1;

foo2 = bar2;

};

means that it is an NSDictionary containing a single entry with key: "args" and value: another NSDictionary.

So, you have successfully read the JSON into Swift.


If you want to see the intermediate form, you may need to access the data reprsenting the response body, and convert it to text,

which you do not need other than for debugging.

Thank you. I didn't know JSON items are converted into those types by Swift.

Is it because of convenience? Is it easier to work with dictionaries in Swift? I'm asking you that because coming from Java I was used to working on pure json formats.

Anyhow I'm left with decoding a dictionary, then.
Something like this, if I'm not mistaken:

let jsonData = response.data
let json = try? JSONSerialization.jsonObject(with: jsonData!, options: []) as? [String: Any]
    if let collection = jsonRoot["key"] as? NSArray {
               //extracting values
}

Is it because of convenience?

Sort of designer's decision. Some JSON frameoworks uses dedicated types to represent JSON items, such as `JSONString`, `JSONNumber`, `JSONArray` or `JSONObject` (or a generic single `JSON` type), and some others maps JSON items to some existing types, such as `List<Object>` for JSON array or `Map<String, Object>` for JSON object. In Java, there are so many JSON frameworks that you may find both designs.


In Alamofire when using `responseJSON`, the latter is used. In fact, it uses an old `JSONSerialization`, written for Objective-C.


Recently, you have another option to work with JSON in Swift, so called `Codable`. Anyway, you may need to show more context to get a concrete description on how to handle NSDictionary or NSArray, or how to handle your JSON response using `Codable`.


Please do not hesitate to start a new thread to find such solutions, including as much examples and your code would be better.

Thank you for the help! It means a lot to a beginner like me.

In the end I've used SwiftyJSON to parse it. Much simpler and cleaner.