[英]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.