如何在Swift中将字典转换为json,并删除" optional"在json字符串中

时间:2015-12-20 15:20:09

标签: json swift dictionary

我想将字典转换为json,字典如下:

[
      [
        "user_event" : "Optional({\n  \"strokes\" : {\n    \"oldText\" : \"er\",\n    \"selection\" : \"error\",\n    \"candidates\" : \"error|errors|era\",\n    \"context\" : \"error \"\n  }\n})",
        "device_type" : "x86_64",
        "language" : "EN_US"
      ],
      [
        "user_event" : "Optional({\n  \"strokes\" : {\n    \"oldText\" : \"\",\n    \"selection\" : \"messages\",\n    \"candidates\" : \"messages|message|in\",\n    \"context\" : \"error messages \"\n  }\n})",
        "device_type" : "x86_64",
        "language" : "EN_US"
      ]
    ]

我使用以下代码:

let data = try NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions())
let json = NSString.init(data: data, encoding: NSUTF8StringEncoding)

但结果字符串的字符为"可选("这将导致json解码器进程失败,如何排除这些附加字符?

0 个答案:

没有答案