本文介绍了如何使用Alamofire和SwiftyJSON访问嵌套的JSON值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用swiftyJSON和Alamofire访问嵌套的JSON结果.我的打印价值为零,我认为我没有正确执行此操作.我的参数应该是什么?我正在尝试获取位于 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 quotes.rest/qod.json
func getAPI() { Alamofire.request(.GET, "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, "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值?
发布评论