json - 使用 Swift 获取 Json 元素

标签 json swift

我在解析相似视频标题时遇到问题。 json 中总会有 1 或 2 部相似的电影,因此我试图将这两个视频存储在两个单独的数组中。

这是我的 JSON 示例:

 {
        "status": "ok",
        "status_message": "Query was successful",
        "data": {
            "limit": 20,
            "movies": [
                {
                    "id": 400,
                    "url": "www.google.com",
                    "title": "Battle for Skyark",
                    "year": 2016,
                    "rating": 2.8,
                    "runtime": 88,
                    "genres": [
                        "Action",
                        "Adventure",
                        "Family",
                        "Sci-Fi"
                    ],
                    "summary": "When a mysterious race of creatures takes over a desolate Earth, humanity must take refuge in SkyArk, the city in the sky. After the rebellion against a corrupt leadership fails, the wealthy doom the rebels' children to live on the ruins of the old Earth. The rebel leader's son, Rags, must lead his fellow exiles against the monsters in order to have a chance to return to SkyArk, but he soon finds that he has a much greater purpose in saving humanity.",
                    "language": "English",
                    "background_image": "background.jpg",
                    "state": "ok",
                    "similarvideos": [
                        {
                            "title": "The Long Road"
                        },
                        {
                            "title": "Boyhood"
                        }
                    ],
                    "date_uploaded": "2015-10-31 21:48:19",
                    "date_uploaded_unix": 1446342499
                }
            }
        }

我能够像这样解析摘要(连同其他元素):

var summarys = [String]()
self.summarys = json.valueForKeyPath("data.movies.summary") as! [String]

这就是我尝试解析类似视频标题的方式,但它不起作用:

self.similarfirstmovies = json.valueForKeyPath("data.movies.similarvideos[0].title") as! [String]
self.similarsecondmovies = json.valueForKeyPath("data.movies.similarvideos[1].title") as! [String]

非常感谢所有帮助。

最佳答案

您可以像这样获取类似的视频:

var similarfirstmovie = json.valueForKeyPath("data.movies[0].similarvideos[0].title") as! String
var similarsecondmovie = json.valueForKeyPath("data.movies[0].similarvideos[1].title") as! String

你的错误是 movies 是数组。首先你应该选择你想要的电影。然后你可以找到那部电影的相似视频。

但是如果你想直接从 json 中获取所有 similarfirstmovies 和 secondmovies。首先你应该在数组中获取 movies 对象,然后你应该编写一个谓词来获取所有 similarvideos。

关于json - 使用 Swift 获取 Json 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569212/

相关文章:

javascript - Ajax - 在 "success"之后转到另一个页面从那里的 url 检索用户 id

javascript - JQuery 每个关联 JSON 对象

javascript - 如何将 jQuery.serialize() 数据转换为 JSON 对象?

java - Gson:我们可以在类型适配器中获取序列化的字段名称吗?

ios - 我希望用户仅在他/她在 swift 3 中点击 uipickerview 时才选择国家

arrays - 如何使用数组中的多个 NSButton 执行功能?

ios - 将数据从 UIViewController 传递到 UISpiltView

javascript - JSON 解析将某个数字转换为其他数字

ios - 使用 Firebase push() 创建列表

ios - "Cannot find an initializer for type ' MKPlaceMark' 接受类型的参数列表