json - 使用 Swift JSON 从 TheMovieDB API 获取数据时遇到问题

标签 json swift themoviedb-api

我是 JSON 的新手,想开始使用一个简单的应用程序,在您输入标题时提供电影概览。我下面的代码返回一个大字符串中的所有内容。如何只获取一条信息,例如概览或年份?

在我下面的尝试中,print(obj["overview"] as Any)) 打印出“nil”并且 print(obj) 看起来像这样:

{
page = 1;
results =     (
            {
        adult = 0;
        "backdrop_path" = "/A0aGxrCGRBuCrDltGYiKGeAUect.jpg";
        "genre_ids" =             (
            53,
            80
        );
        id = 680;
        "original_language" = en;
        "original_title" = "Pulp Fiction";
        overview = "A burger-loving hit man, his philosophical partner, a drug-addled gangster's moll and a washed-up boxer converge in this sprawling, comedic crime caper. Their adventures unfurl in three stories that ingeniously trip back and forth in time.";

当前代码:

    let query = "Pulp+Fiction"
    let urlString = "https://api.themoviedb.org/3/search/movie?api_key={MYAPIKEY}&query=\(query)"

    let url = URL(string: urlString)
    URLSession.shared.dataTask(with:url!) { (data, response, error) in
        if error != nil {
            print(error as Any)
        } else {
            do {
                let parsedData = try JSONSerialization.jsonObject(with: data!) as Any
                if let obj = parsedData as? NSDictionary {

                    print(obj["overview"] as Any)
                    print(obj)

                }
            } catch {
                print("error")
            }            }
        }.resume()
}

最佳答案

    // write this extension anywhere in your any swift file
    extension String{
         func toDictionary() -> NSDictionary {
          let blankDict : NSDictionary = [:]
         if let data = self.data(using: .utf8) {
            do {
               return try JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
                       } catch {
                            print(error.localizedDescription)
                        }
                    }
                    return blankDict
                }
            }
      //now  in your code modify as 
        if  data != nil {
                        let responseString = String(data: data!, encoding: .utf8)!
                        if(responseString != "")
                        {
                         //convert response string into dictionary using extended method
                         let responseValues = responseString.toDictionary()
                         //access value using keyPath using
                         let value = responseValues.value(forKeyPath: "key.key2")
//where key2 is the target key which is inside the value of key 
                     }
        }

关于json - 使用 Swift JSON 从 TheMovieDB API 获取数据时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284944/

相关文章:

ruby-on-rails - 使用 activerecord 在 jsonb postgres 中查询嵌套的哈希数组是否存在 key

TMDB 包装器 API 中的 Swift 函数不清楚

ios - 有两个选择的数组让我只完成了一半

java - 使用 themoviedb 获取 401,并进行改造

javascript - 使用 JsonResult 和 JavaScript 解析 JSON

Javascript按属性分组对象

PHP mysql json 谷歌图表

iOS:CloudKit perform(query:) 什么都不做 - 闭包未执行

ios - 当前 View Controller nil key 错误