【发布时间】:2019-01-08 15:14:34
【问题描述】:
这是我的 Json:
[ {
"id": 6854,
"name": "Laundry Iron",
"images": [
{
"id": 6856,
"src": "https://abcd.com/yzx/uploads/1750.jpg",
}
],
} ]
我们如何制作模型类来获取 "images":["src": "String" ]?我想抓住我尝试过的“src”,但它不起作用:
class ProductModel {
var title: String?
var regularPrice: Int?
var salePrice: Int?
var productDescroption: String?
var imageUrl: [ImageUrl]?
init(productJsonL: NSDictionary) {
self.title = productJsonL["name"] as? String
self.regularPrice = productJsonL["regular_price"] as? Int
self.salePrice = productJsonL["sale_price"] as? Int
self.productDescroption = productJsonL["description"] as? String
//The problem is here ........
//self.imageUrl = productJsonL["images"]![0]!["src"] as? String
self.imageUrl = ImageUrl(imageUrlJson: (productJsonL["images"]![0] as? NSDictionary)!)
}
}
class ImageUrl {
var source: String?
init(imageUrlJson: NSDictionary) {
self.source = imageUrlJson["src"] as? String
}
}
请像我上面所做的那样纠正我的结构,以便我可以一次将所有内容附加到数组中?提前谢谢你!
【问题讨论】:
-
ProductModel和你的JSON几乎没有共同点,它是从哪里来的?