gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:41:55 24 4
gpt4 key购买 nike

我正在尝试将 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"]
}]

电影类:

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
}
}

最佳答案

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 数据源

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

关于ios - 如何解析具有多个值的 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44860791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com