apple change my JSON format after using URLSession.shared.dataTask

hello,


I am green to swift 3. In order to transfer data between mysql server and ios, i try to connect my server and device by URLSession.Shared.dataTask.


but the format changed form


[{"id":"1","names":"abc","pws_1":"password","pws_2":"pw"}]


to


Optional(<__NSSingleObjectArrayI 0x6200000039e0>(
{
    names = abc;
    id = 1;
    "pws_1" = password;
    "pws_2" = pw;
}
)
)


how can i convert the json object?

Replies

It’s hard to say for sure without seeing more of your code. Here’s a snippet of how you might send a basic JSON request to a server.

import Foundation

let json: [[String:String]] = [
    [ "tick": "tock" ],
    [ "bim": "bam" ]
]

let session = URLSession.shared
let url = URL(string: "http://example.com")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try! JSONSerialization.data(withJSONObject: json, options: [])
let task = session.dataTask(with: request) { (responseBody, response, error) in
    // … check `error` then check `response` then process `responseBody` …
}
task.resume()

You need to look at how your code sets the

httpBody
property. It seems like you’re serialising the in-memory objects to JSON data in some non-standard way.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"