首页 新闻 会员 周边 捐助

Swift的JSON解析库提示Json字符串格式不对

0
悬赏园豆:20 [已关闭问题] 关闭于 2021-01-27 22:41

提示警告:"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)

问题补充:

这才是原先的字符串,网站编辑器把特殊字符\弄没了

PER10的主页 PER10 | 初学一级 | 园豆:8
提问于:2021-01-27 22:22
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册