faheem faheem -4 years ago 86
iOS Question

Unable to convert this JSON to a string

Please see below the json output

{
"queryLogs" : [
{
"status" : "false",
"query" : {
"contents" : {
"updated" : "",
"id" : 1488199579,
"created" : "",
"patient_count" : 60,
"isactive" : "1",
"status_id" : 0,
"starttime" : "",
"queue_status_id" : 0,
"date_consult" : ""
},
"conditions" : "{}"
},
"tableName" : "consultation",
"type" : "I",
"logId" : {
"id" : "261489537666",
"doctorId" : "100"
}
}
]
}


Need to convert above json into below format

{"queryLogs":[{ "logId":{"id":"76148951287","doctorId":"100"},
"tableName":"queue", "type":"I", "query":"{ \"contents\":{
\"patient_name\":\"queryLog Test\", \"status_id\":1,
\"queue_no\":\"6\", \"isactive\":1, \"id\":\"148956612\",
\"mobile\":\"9567969610\", \"updated\":\"2017-03-15 11:31:26
GMT+05:30\", \"created\":\"2017-03-15 11:31:26 GMT+05:30\",
\"consultation_id\":\"1495085636\"}, \"conditions\":{}
}","status":"false"}]}


First code is what i get when i convert the JSON, But how can i get the JSON like the second code.
i used below code to get the first output.

var f = ["queryLogs":[["status":"false","tableName":"consultation","type":"I","logId":ids,"query":logfile]]] as [String : Any]
let JSON = try? JSONSerialization.data(withJSONObject: f,
options:.prettyPrinted)
if let content = String(data: JSON!, encoding:
String.Encoding.utf8) {

print(content)
}

Answer Source

If you want response like that then you need to also make JSON string for your logfile dictionary also.

What you can do is make one extension of Dictionary, so that no need to write same code of JSONSerialization at every place.

extension Dictionary where Key: ExpressibleByStringLiteral {
    var jsonString: String? {
        guard let data = try? JSONSerialization.data(withJSONObject: self),
            let string = String(data: data, encoding: .utf8) else {
                return nil
        }
        return string
    }
}

Now use this extension to get JSON string from your dictionaries.

let id‌​s = ["id" : "261489537666", "doctorId" : "100"]
let  logfile =   [
        "contents" : [
            "updated" : "",
            "id" : 1488199579,
            "created" : "",
            "patient_count" : 60,
            "isactive" : "1",
            "status_id" : 0,
            "starttime" : "",
            "queue_status_id" : 0,
            "date_consult" : ""
        ],
        "conditions" : "{}"
] as [String : Any]

if let queryLogString = logfile.jsonString {
    let f = ["queryLogs":[["status":"false","tableName":"consultation","‌​type":"I","logId": id‌​s,"query":queryLogString]]] as [String : Any]
    if let content = f.jsonString {
        print(content)

    }
}

Output:

{"queryLogs":[{"status":"false","query":"{\"contents\":{\"updated\":\"\",\"id\":1488199579,\"created\":\"\",\"patient_count\":60,\"isactive\":\"1\",\"status_id\":0,\"starttime\":\"\",\"queue_status_id\":0,\"date_consult\":\"\"},\"conditions\":\"{}\"}","tableName":"consultation","‌​type":"I","logId":{"id":"261489537666","doctorId":"100"}}]}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download