无法将此JSON转换为字符串

时间:2017-05-19 05:54:19

标签: ios json swift3 xcode8

 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"
       }
      }
     ]
    }

需要将上面的json转换为以下格式

 {"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"}]}

第一个代码是我转换JSON时得到的,但是我怎样才能获得像第二个代码那样的JSON。 我使用下面的代码来获得第一个输出。

  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)
    }

2 个答案:

答案 0 :(得分:1)

如果您想要这样的回复,那么您还需要为JSON词典制作logfile字符串。

您可以做的是extension Dictionary JSONSerialization,因此无需在每个地方编写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 } } 的相同代码。

JSON

现在使用此扩展程序从词典中获取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) } } 字符串。

{"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"}}]}

<强>输出:

attachHandlers

答案 1 :(得分:0)

一旦尝试,

  let data  = try? JSONSerialization.data(withJSONObject: dic, options: JSONSerialization.WritingOptions(rawValue: 0))

    if let content = String(data: data!, encoding:
        String.Encoding.utf8) {

        print(content)
    }