json - 解析为json字符串正确格式

标签 json swift

我在字典中有一个字符串,我试图将其传递给 jsonstring 但它没有删除反斜杠,并且服务器不会吞咽它。有什么想法吗?

let myBod = "\"Body\": {" +
        "\"type\": \"multipart\"," +
        "\"content\": [" +
        "{" +
        "\"contentType\": \"multipart/alternative; Boundary=\\\"0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\\\"\"," +
        "\"contentDisposition\": \"inline\"" +
         " }," +
         "{" +
         "\"contentType\": \"text/plain; charset=US-ASCII\"," +
        "\"data\": \"yappy\"," +
         "\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"" +
         " }," +
        " {" +
        "\"contentType\": \"text/html; charset=US-ASCII\"," +
          "\"contentDisposition\": \"inline\"," +
         "\"data\": \"<html><body>yappy</body></html>\"," +
         "\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"}]}"

编辑:

if let json = try? JSONSerialization.data(withJSONObject: dataToPost, options: []) {
    if let contentJSONString = String(data: json, encoding: String.Encoding.utf8) {
        // here `content` is the JSON dictionary containing the String
        dataPost.append(contentJSONString)
        print(contentJSONString)
    }
}

这会返回我的字符串,但不会消失反斜杠。

JSON string = {"_Abstract":"Problema al instalar Historian 6.0 Enterprise  Info Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24","Executor":"","Watcher":[""],"Supervisor":"César Ramos","Name":"NOVTRACERT-24","Body":"{\"type\": \"multipart\",\"content\": [{\"contentType\": \"multipart\/alternative; Boundary=\\\"0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\\\"\",\"contentDisposition\": \"inline\" },{\"contentType\": \"text\/plain; charset=US-ASCII\",\"data\": \"Problema al instalar Historian 6.0 Enterprise\r\n\r\nInfo Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24\",\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\" }, {\"contentType\": \"text\/html; charset=US-ASCII\",\"contentDisposition\": \"inline\",\"data\": \"<html><body>Problema al instalar Historian 6.0 Enterprise\r\n\r\nInfo Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24<\/body><\/html>\",\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"}]}"}

最佳答案

我猜测这就是您看到的输出:

{
    "_Abstract": "Problema al instalar Historian 6.0 Enterprise  Info Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24",
    "Executor": "",
    "Watcher": [""],
    "Supervisor": "César Ramos",
    "Name": "NOVTRACERT-24",
    "Body": "{\"type\": \"multipart\",\"content\": [{\"contentType\": \"multipart\/alternative; Boundary=\\\"0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\\\"\",\"contentDisposition\": \"inline\" },{\"contentType\": \"text\/plain; charset=US-ASCII\",\"data\": \"Problema al instalar Historian 6.0 Enterprise\r\n\r\nInfo Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24\",\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\" }, {\"contentType\": \"text\/html; charset=US-ASCII\",\"contentDisposition\": \"inline\",\"data\": \"<html><body>Problema al instalar Historian 6.0 Enterprise\r\n\r\nInfo Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24<\/body><\/html>\",\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"}]}"
}

我运行了JSON Lint来美化它并检查它是否是有效的 JSON。 body 项是一个恰好(在本例中)包含 JSON 的字符串,但由于它在另一段 JSON 中序列化为字符串,因此需要对所有 JSON 特殊字符进行转义(即 "\)。这就是您所要求的,并且编码正确。

如果您希望 body 成为 JSON 对象而不是字符串,则需要首先通过反序列化将其转换为 JSON,然后设置 body 键然后序列化整个消息。

关于json - 解析为json字符串正确格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42853379/

相关文章:

json - 使用 Circe 将 JSON 数组解析为案例类

json - 将 JSON 文件导入 Logstash + Elasticsearch + Kibana

ios - 使用 Swift 重建现有应用程序

swift - 如何修复逃逸闭包?错误是: Converting non-escaping value may allow it to escape

json - 快速播放列表播放选定的歌曲

C# 序列化字典 <ulong,ulong> 到 JSON

json - 使用 JSON 模式进行 DRY 用户输入验证(客户端、服务器端)

javascript - 我如何从对象(wordpress)读取自定义字段

swift - 正确使用 Swift Guard 关键字?

ios - 如何启用/禁用特定文本字段的 OneTimeCode 功能?