提示警告:"The given data was not valid JSON."
代码如下:
struct Person:Codable{
let name:String
let age:Int
}
let jsonData = "{"name":"Tansion","age",10}".data(using: .utf8)!
let decoder = JSONDecoder()
let o = try! decoder.decode(Person.self, from: jsonData) //在这里提示警告
print("name="+o.name)
//------------------------------------------------------------------
具体警告:
Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No value for key in object around character 23." UserInfo={NSDebugDescription=No value for key in object around character 23.}))): file Test12/ViewController.swift, line 35
2021-01-27 06:00:15.524921-0800 Test12[6034:323712] Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No value for key in object around character 23." UserInfo={NSDebugDescription=No value for key in object around character 23.}))): file Test12/ViewController.swift, line 35
(lldb)
这才是原先的字符串,网站编辑器把特殊字符\弄没了