如何解析具有多个值的 json?

Posted

技术标签:

【中文标题】如何解析具有多个值的 json?【英文标题】:How do I parse json with more then one values? 【发布时间】:2017-07-01 12:12:07 【问题描述】:

我正在尝试将 JSON 文件解析为我的代码。 所以,到目前为止,我已经设法做到了,除了一个具有一组值的键(流派)。 我想知道如何解析它,以便可以在UILabel 中呈现它?

文件中的 JSON 数据:

  [
        "title": "Dawn of the Planet of the Apes",
        "image": "http://api.androidhive.info/json/movies/1.jpg",
        "rating": 8.3,
        "releaseYear": 2014,
        "genre": ["Action", "Drama", "Sci-Fi"]
    ,
    
        "title": "District 9",
        "image": "http://api.androidhive.info/json/movies/2.jpg",
        "rating": 8,
        "releaseYear": 2009,
        "genre": ["Action", "Sci-Fi", "Thriller"]
    ,
    
        "title": "Transformers: Age of Extinction",
        "image": "http://api.androidhive.info/json/movies/3.jpg",
        "rating": 6.3,
        "releaseYear": 2014,
        "genre": ["Action", "Adventure", "Sci-Fi"]
    ,
    
        "title": "X-Men: Days of Future Past",
        "image": "http://api.androidhive.info/json/movies/4.jpg",
        "rating": 8.4,
        "releaseYear": 2014,
        "genre": ["Action", "Sci-Fi", "Thriller"]
    ,
    
        "title": "The Machinist",
        "image": "http://api.androidhive.info/json/movies/5.jpg",
        "rating": 7.8,
        "releaseYear": 2004,
        "genre": ["Drama", "Thriller"]
    ,
    
        "title": "The Last Samurai",
        "image": "http://api.androidhive.info/json/movies/6.jpg",
        "rating": 7.7,
        "releaseYear": 2003,
        "genre": ["Action", "Drama", "History"]
    ,
    
        "title": "The Amazing Spider-Man 2",
        "image": "http://api.androidhive.info/json/movies/7.jpg",
        "rating": 7.3,
        "releaseYear": 2014,
        "genre": ["Action", "Adventure", "Fantasy"]
    ,
    
        "title": "Tangled",
        "image": "http://api.androidhive.info/json/movies/8.jpg",
        "rating": 7.9,
        "releaseYear": 2010,
        "genre": ["Action", "Drama", "Sci-Fi"]
    ,
    
        "title": "Rush",
        "image": "http://api.androidhive.info/json/movies/9.jpg",
        "rating": 8.3,
        "releaseYear": 2013,
        "genre": ["Animation", "Comedy", "Family"]
    ,
    
        "title": "Drag Me to Hell",
        "image": "http://api.androidhive.info/json/movies/10.jpg",
        "rating": 6.7,
        "releaseYear": 2009,
        "genre": ["Horror", "Thriller"]
    ,
    
        "title": "Despicable Me 2",
        "image": "http://api.androidhive.info/json/movies/11.jpg",
        "rating": 7.6,
        "releaseYear": 2013,
        "genre": ["Animation", "Comedy", "Family"]
    ,
    
        "title": "Kill Bill: Vol. 1",
        "image": "http://api.androidhive.info/json/movies/12.jpg",
        "rating": 8.2,
        "releaseYear": 2003,
        "genre": ["Action", "Crime"]
    ,
    
        "title": "A Bug's Life",
        "image": "http://api.androidhive.info/json/movies/13.jpg",
        "rating": 7.2,
        "releaseYear": 1998,
        "genre": ["Animation", "Adventure", "Comedy"]
    ,
    
        "title": "Life of Brian",
        "image": "http://api.androidhive.info/json/movies/14.jpg",
        "rating": 8.9,
        "releaseYear": 1972,
        "genre": ["Comedy"]
    ,
    
        "title": "How to Train Your Dragon",
        "image": "http://api.androidhive.info/json/movies/15.jpg",
        "rating": 8.2,
        "releaseYear": 2010,
        "genre": ["Animation", "Adventure", "Family"]
    ]

Movie类:

import UIKit
class Movie 
    var title = ""
    var image = ""
    var rating = 0.0
    var releaseYear = 0

    init(object: [String: AnyObject]) 
        self.title = object["title"] as! String
        self.image = object["image"] as! String
        self.rating = object["rating"] as! Double
        self.releaseYear = object["releaseYear"] as! Int


    

    init() 

    

MovieCell班级:

import UIKit
import SDWebImage

class MovieCell: UITableViewCell 


    @IBOutlet weak var titleLbl: UILabel!
    @IBOutlet weak var releaseYearLbl: UILabel!
    @IBOutlet weak var ratingLbl: UILabel!
    @IBOutlet weak var movieImage: UIImageView!
    @IBOutlet weak var genreLbl: UILabel!

    override func awakeFromNib() 
        super.awakeFromNib()

    

    func createCell(movie: Movie) 
        self.titleLbl.text = movie.title
        self.releaseYearLbl.text = "\(movie.releaseYear)"
        self.ratingLbl.text = "\(movie.rating) ⭐️"

        movieImage.layer.cornerRadius = 10.0
        movieImage.clipsToBounds = true

        self.movieImage.sd_setImage(with: URL(string: movie.image))

    

    override func setSelected(_ selected: Bool, animated: Bool) 
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    

【问题讨论】:

【参考方案1】:

Swift 3.0

只需分析以下代码并将其应用于您的问题

Movie 类变为,

class Movie 

    var title = ""
    var image = ""
    var rating = 0.0
    var releaseYear = 0
    var genre:String?

    init(object: [String: AnyObject]) 
        self.title = object["title"] as! String
        self.image = object["image"] as! String
        self.rating = object["rating"] as! Double
        self.releaseYear = object["releaseYear"] as! Int
        let genreArray = object["genre"] as! [String]

        /*Whatever you want do here with genre. I thought you need whole array to one readable String*/
        for strings in genreArray

            self.genre = self.genre == nil ? strings : self.genre!+" "+strings
        

    

UITableView的类

类的全局变量是,

var movieData = Array<Movie>()

JSON 解析会变成,

if let json = try! JSONSerialization.jsonObject(with: data, options: .allowFragments) as? Array<Dictionary<String, AnyObject>>

for object in json

    self.movieData.append(Movie(object: object))


DispatchQueue.main.async 
    self.tableView.reloadData()

UITableView datasource

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

    return movieData.count


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

    let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)

    let tableObject:Movie = self.movieData[indexPath.row]
    cell.textLabel?.text = tableObject.genre //This way you will be get all values and apply it to whatever you want...
    return cell

输出:-

【讨论】:

以上是关于如何解析具有多个值的 json?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Swifty 动态创建具有多个键和值的 json 对象

如何在Java中解析具有空值的JSON对象?

解析文件并将其存储到具有Node js中多个值的Map函数中

如何在 R 中解析具有堆叠多个 JSON 的文件?

如何在对象 C 中解析具有多个实例的 JSON [重复]

使用 Spring boot 解析具有无效键值的 JSON 对象