How do I access a nested JSON value using Alamofire and SwiftyJSON?

前端 未结 2 792
误落风尘
误落风尘 2021-01-15 06:25

I am trying to access nested JSON results using swiftyJSON and Alamofire. My print value is nill and I believe I am not doing this correctly. What should my parameters be? I

2条回答
  •  伪装坚强ぢ
    2021-01-15 06:51

    In your JSON quotes is an array so if you want to access quote of the first object you should do it by accessing first object:

     func getAPI() {
            Alamofire.request(.GET, "http://quotes.rest/qod.json", parameters: ["contents": "quotes"])
                .responseJSON { response in
                    if let jsonValue = response.result.value {
                        let json = JSON(jsonValue)
                        if let quote = json["contents"]["quotes"][0]["quote"].string{
                         print(quote)
                        }
                    }
            }
        }
    

提交回复
热议问题