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?
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.
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.
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the
Apple Developer Forums Participation Agreement.
Forums
Apple Developer Program
Apple Developer Enterprise Program
App Store Small Business Program
MFi Program
News Partner Program
Video Partner Program
Security Bounty Program
Security Research Device Program