如何用字典为json数据编写模型类?

时间:2017-10-06 05:50:28

标签: ios swift3

在这个类中我已经编写了模型类但是这里已经添加了一些新的数据,但是我正在尝试为模型类创建剩余的字典但是无法实现它可以帮助我如何实现它吗?

在这已经实现了items数组的模型类,这里我需要为搜索条件字典和内部数组创建一个模型类

enter image description here

 func listCategoryDownloadJsonWithURL() {
        let url = URL(string: listPageUrl)!
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            if error != nil { print(error!); return }
            do {
                if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [String:Any] {
                    let objArr = jsonObj["items"] as? [[String:Any]]
                    self.list = objArr!.map{List(dict: $0)}
                    DispatchQueue.main.async {
                        let itemsCount = self.list.count
                        for i in 0..<itemsCount {
                            let customAttribute = self.list[i].customAttribute
                            for j in 0..<customAttribute.count {
                                if customAttribute[j].attributeCode == "image" {
                                    let baseUrl = "http://192.168.1.11/magento2/pub/media/catalog/product"
                                    self.listCategoryImageArray.append(baseUrl + customAttribute[j].value)
                                }
                            }
                        }
                        self.activityIndicator.stopAnimating()
                        self.activityIndicator.hidesWhenStopped = true
                        self.collectionView.reloadData()
                        self.collectionView.isHidden = false
                        self.tableView.reloadData()
                    }
                }
            } catch {
                print(error)
            }
        }
        task.resume()
    }
struct List {

    let name : String
    let sku : Any
    let id : Int
    let attributeSetId : Int
    let price : Int
    let status : Int
    let visibility : Int
    let typeId: String
    let createdAt : Any
    let updatedAt : Any
    var customAttribute = [ListAttribute]()


    init(dict : [String:Any]) {
        if let customAttribute = dict["custom_attributes"] as? [[String: AnyObject]] {
            var result = [ListAttribute]()
            for obj in customAttribute {
                result.append(ListAttribute(json: obj as! [String : String])!)
            }
            self.customAttribute = result
        } else {
            self.customAttribute = [ListAttribute]()
        }

        self.name = (dict["name"] as? String)!
        self.sku = dict["sku"]!
        self.id = (dict["id"] as? Int)!
        self.attributeSetId = (dict["attribute_set_id"] as? Int)!
        self.price = (dict["price"] as? Int)!
        self.status = (dict["status"] as? Int)!
        self.visibility = (dict["visibility"] as? Int)!
        self.typeId = (dict["type_id"] as? String)!
        self.createdAt = dict["created_at"]!
        self.updatedAt = dict["updated_at"]!
    }
}
struct ListAttribute {

    let attributeCode : String
    let value : String

    init?(json : [String:String]) {

        self.attributeCode = (json["attribute_code"])!
        self.value = (json["value"])!

    }
}

0 个答案:

没有答案