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)
}
关于ios - Swift - 解析嵌套 JSON 对象中的数组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46801415/