0

I am new to the iOS development , I have dictionary as given below, I want to convert it to JSON, pass it to API

["checkinDate": "",
"data": [{
    "abc": 2,
    "cde": 13,
    "edt": 1,
    "shortName": "OCC"
}],
"incidentDescription": "",
"store": "ABD"

]

Code:

  let jsonData = try! JSONSerialization.data(withJSONObject: saveDict)
  let jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)

I have used below code but it is giving output as

"materialList": ["{\"abc\":2,\"cde\":13,\"edt\":1,\"shortName\":\"OCC\"}"],
"checkinDate": "",
"incidentDescription": "",

It is add \ while converting array of dictionary to JSON

  • I have also tried. if let theJSONData = try? JSONSerialization.data( withJSONObject: saveDict, options: .prettyPrinted ), let theJSONText = String(data: theJSONData, encoding: .utf8) { print("JSON string = \n\(theJSONText)") } – Priyanka Kadam Mar 15 '20 at 18:36
  • *Convert Dictionary*. What dictionary? The *dictionary as given below* is neither valid JSON nor valid Swift dictionary. The added backslashes are normal (and virtual) to be able to display double quotes in a literal string – vadian Mar 15 '20 at 18:45
  • i think https://stackoverflow.com/a/30480777/4757272 should answer you question. Good luck – Vollan Mar 15 '20 at 19:05
  • I’d recommend using the `codable protocoll` since it’s much easier to code & decode JSON data – Putte Mar 15 '20 at 19:57
  • The input data you have given doesn't match the output but apart from that the \" is expected behaviour as already stated so what is this question about? – Joakim Danielson Mar 15 '20 at 21:10

0 Answers0