如何使用Alamofire和SwiftyJSON访问嵌套的JSON值? [英] How do I access a nested JSON value using Alamofire and SwiftyJSON?

查看:93
本文介绍了如何使用Alamofire和SwiftyJSON访问嵌套的JSON值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用swiftyJSON和Alamofire访问嵌套的JSON结果.我的打印价值为零,我认为我没有正确执行此操作.我的参数应该是什么?我正在尝试获取位于 http://quotes.rest/qod.json 的报价值

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 am trying to get the quote value located at http://quotes.rest/qod.json

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

            }
    }
}

推荐答案

在JSON中,quotes是一个数组,因此,如果要访问第一个对象的quote,则应通过访问第一个对象来实现:

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)
                    }
                }
        }
    }

这篇关于如何使用Alamofire和SwiftyJSON访问嵌套的JSON值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆