【发布时间】:2021-01-30 09:20:19
【问题描述】:
我有一个 json 字符串。但我无法将其更改为 Dictionary String:Any 。谁能帮帮我?
这是我的代码
var str = "{\"01-2021\":\"[\"01 06 2021\",\"01 07 2021\",\"01 13 2021\",\"01 14 2021\",\"01 20 2021\",\"01 21 2021\",\"01 27 2021\",\"01 28 2021\"]\",\"02-2021\":\"[\"02 03 2021\",\"02 04 2021\",\"02 10 2021\",\"02 11 2021\",\"02 17 2021\",\"02 18 2021\",\"02 24 2021\",\"02 25 2021\"]\"}"
let data = string.data(using: .utf8)!
do {
if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .fragmentsAllowed) as? [String: Any]
{
print(jsonArray) // use the json here
} else {
print("bad json")
}
} catch let error as NSError {
print(error)
}
我总是收到这样的错误:Error Domain=NSCocoaErrorDomain Code=3840“字符 14 周围的对象格式错误。” UserInfo={NSDebugDescription=字符 14 周围的对象格式错误。}
【问题讨论】:
-
我想转换成字典,例如 ["01-2021" : "["01 06 2021","01 07 2021","01 13 2021","01 14 2021","01 20 2021","01 21 2021","01 27 2021","01 28 2021"]", [02-2021" : "["02 03 2021","02 04 2021","02 10 2021", "02 11 2021","02 17 2021","02 18 2021","02 24 2021","02 25 2021"]"]
标签: ios