ios - Swift - 解析嵌套 JSON 对象中的数组不起作用

标签 ios json swift parsing dictionary

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/

相关文章:

Swift - deinit 方法中的 self

iOS 10、Swift 3.0、动画不流畅

android - Kotlin Native 编译 jar 和框架

javascript - toString 所有对象数据

ios - 谷歌地图IOS sdk只显示 '??????'

json - 如何从 HTTP 响应正文中读取错误

javascript - Blogger 最近的帖子控制台错误 : Cannot read property 'title' of undefined

ios - 限制 UISwipeGestureRecognizer 区域

ios - 如何通过一个按钮链接两个 iphone 应用程序

objective-c - 在Objective-C中替换字符串中的一个字符