如何使用多个值解析json?

时间:2017-07-01 12:12:07

标签: ios arrays json swift

我试图将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 个答案:

答案 0 :(得分:2)

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
}

<强>输出: -

enter image description here