[英]Swift - Parse array in nested JSON Object not working
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [.allowFragments])
if let responseJSON = responseJSON as? [String:Any] {
if let tJsonObj = xResponse["d"] as? [[String:Any]] {
// not working here...
}
}
tJsonObj变量未获取我的json数组内容。 我的json看起来像这样:
{"d": "[{\"title\":\"xxx\",\"timestamp\":\"2017-10-16 23:53:40\"},{\"title\":\"Mein Test iPhone 7\",\"timestamp\":\"2017-10-17 18:16:24\"}]"}
希望有人能提供帮助-谢谢!
密钥d
的值是另一个JSON字符串。 您需要两次使用JSONSerialization
do {
if let responseJSON = try JSONSerialization.jsonObject(with: data) as? [String:Any],
let tJsonObj = responseJSON["d"] as? String {
if let innerJSON = try JSONSerialization.jsonObject(with: Data(tJsonObj.utf8)) as? [[String:Any]] {
for item in innerJSON {
print(item)
}
}
}
} catch {
print(error)
}
d
的内部JSON看起来已经转义。 有效的JSON应该类似于:
{"d": "[{"title":"xxx","timestamp":"2017-10-16 23:53:40"},...
您的JSON来自哪里?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.