如何使用Alamofire和SwiftyJSON访问嵌套的JSON值?

编程入门 行业动态 更新时间:2024-10-13 14:28:11
本文介绍了如何使用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值?

本文发布于:2023-11-26 02:26:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   如何使用   SwiftyJSON   Alamofire   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!