访问嵌套字典swift 3

时间:2017-05-31 03:18:45

标签: swift

我发出POST请求,我得到了这个回复

{
  "id": "6QRqYlqpv",
  "hostname": "twitter.com",
  "link": "https://twitter.com/AliNatham/status/869551271409328129",
  "title": "869551271409328129",
  "filename": "869551271409328129.mp4",
  "originalFormat": "mp4",
  "originalType": "video",
  "formats": [
    {
      "format": "mp4",
      "type": "video",
      "url": "https://loadercdn.io/download?id=6QRqYlqpv&format=mp4",
      "directUrl": "https://loadercdn.io/download?id=6QRqYlqpv&direct=true&format=mp4",
      "directUrl_": "directUrl will be deprecated soon, do not use",
      "filename": "869551271409328129.mp4"
    },
    {
      "format": "avi",
      "type": "video",
      "url": "https://loadercdn.io/download?id=6QRqYlqpv&format=avi",
      "directUrl": "https://loadercdn.io/download?id=6QRqYlqpv&direct=true&format=avi",
      "directUrl_": "directUrl will be deprecated soon, do not use",
      "filename": "869551271409328129.avi"
    }]
}

我试图访问"格式":" mp4"像这样 print(json ["格式"]?[" url"]为Any) 但我没有了

2 个答案:

答案 0 :(得分:0)

您的formats是一个数组。您需要先访问数组中的嵌套对象,然后访问url

尝试print(json["formats"][0]?["url"] as Any)

答案 1 :(得分:0)

我解决了!

let formats = json["formats"] as! Array<[String:AnyObject]>
print(test[0]["url"] as! String)