Invalid JSON while parsing \n (quoted) string

孤街醉人 提交于 2019-12-02 11:42:15

问题


I have a problem with a Codable parsing... that's my example code:

class Test: Codable {
      let resultCount: Int?
      let quote: String?
}

 var json =  """
{
    "resultCount" : 42,
    "quote" : "My real quote"
}
""".data(using: .utf8)!

 var decoder = JSONDecoder()
 let testDecoded = try! decoder.decode(Test.self, from: json)

Here everything works as expected and the Test object is created.

Now my back end sends me the quote string with a quote in the middle... in this form (please note \"real\"):

class Test: Codable {
      let resultCount: Int?
      let quote: String?
}

 var json =  """
{
    "resultCount" : 42,
    "quote" : "My \"real\" quote"
}
""".data(using: .utf8)!

 var decoder = JSONDecoder()
 let testDecoded = try! decoder.decode(Test.self, from: json)

In this second case the decoder fails to create the object... and that's my error message:

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 4." UserInfo={NSDebugDescription=No string key for value in object around character 4.})))

Is there a way to solve this issue?


回答1:


To include quotation marks in your JSON there have to be actual \ characters before the quotation marks within the string:

{
    "resultCount" : 42,
    "quote" : "My \"real\" quote"
}

To do that in Swift string literal, you need to escape the \. That results in "My \\"real\\" quote" within the Swift multi-line string literal:

let json = """
    {
        "resultCount" : 42,
        "quote" : "My \\"real\\" quote"
    }
    """.data(using: .utf8)!

let decoder = JSONDecoder()
let testDecoded = try! decoder.decode(Test.self, from: json)

However, if dealing with a standard, non-multiline string literal, you need to escape both the backslash and the quotation mark, resulting in even more confusing looking \"My \\\"real\\\" quote\":

let json = "{\"resultCount\": 42, \"quote\" : \"My \\\"real\\\" quote\"}"
    .data(using: .utf8)!


来源:https://stackoverflow.com/questions/54041146/invalid-json-while-parsing-n-quoted-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!