简体   繁体   中英

How to Store JSON data into model class array in Swift 5

Hi i am beginner for swift ios and my requirement is have to display Json response to table list i got response from web-services and response seems like below

MY requirement is how to map that model classes to Array and how to display them in tableList can some one help me please

MY RESPONSE

[{
        "id": 6,
        "products": {
            "items": [{
                    "status": 1,
                    "custom_attributes": [

                        {
                            "attribute_code": "short_description",
                            "value": "short_description"
                        },
                        {
                            "attribute_code": "meta_title",
                            "value": "meta_title"
                        },
                        {
                            "attribute_code": "meta_keyword",
                            "value": "meta_keyword"
                        }
                    ]
                },
                {
                    "status": 1,
                    "custom_attributes": [

                        {
                            "attribute_code": "short_description",
                            "value": "short_description"
                        },
                        {
                            "attribute_code": "meta_title",
                            "value": "meta_title"
                        },
                        {
                            "attribute_code": "meta_keyword",
                            "value": "meta_keyword"
                        }
                    ]

                }
            ]
        }
    },
    {
        "id": 6,
        "products": {
            "items": [{
                    "status": 1,
                    "custom_attributes": [

                        {
                            "attribute_code": "short_description",
                            "value": "short_description"
                        },
                        {
                            "attribute_code": "meta_title",
                            "value": "meta_title"
                        },
                        {
                            "attribute_code": "meta_keyword",
                            "value": "meta_keyword"
                        }
                    ]
                },
                {
                    "status": 1,
                    "custom_attributes": [

                        {
                            "attribute_code": "short_description",
                            "value": "short_description"
                        },
                        {
                            "attribute_code": "meta_title",
                            "value": "meta_title"
                        },
                        {
                            "attribute_code": "meta_keyword",
                            "value": "meta_keyword"
                        }
                    ]
                }
            ]
        }
    }
]

I tried this


class DashboardProduct: NSObject {

    class dashProductBook : NSObject {

        static var products : [Product]!
        //MARK: Init
        @discardableResult init(dictionary: [String:Any]){

            dashProductBook.products = [Product]()
            if let dashProductArray = dictionary["products"] as? [[String:Any]]{
                for dic in dashProductArray{
                    let value = Product(dictionary: dic)
                    dashProductBook.products.append(value)
                }
            }

        }

        //MARK: Delete
        static func deleteFromModel()
        {
            products = nil

        }

        //MARK: Return data
        static func toDictionary() -> [String:Any]
        {
            var dictionary = [String:Any]()
            if products != nil{
                var dictionaryElements = [[String:Any]]()
                for productElement in products {
                    dictionaryElements.append(productElement.toDictionary())
                }
                dictionary["products"] = dictionaryElements
            }
            return dictionary
        }
    }

    class Product : NSObject{


        var items : [Items]!

        init(dictionary: [String:Any]) {

            items = [Items]()
            if let regionArray = dictionary["items"] as? [[String:Any]]{
                for dic in regionArray{
                    let value = Items(dictionary: dic)
                    items.append(value)
                }
            }

        }

        func toDictionary() -> [String:Any]
        {
            var dictionary = [String:Any]()

            if items != nil{
                var dictionaryElements = [[String:Any]]()
                for itemElement in items {
                    dictionaryElements.append(itemElement.toDictionary())
                }
                dictionary["items"] = dictionaryElements
            }

            return dictionary
        }
    }


    class Items : NSObject{

        var custom_Attr : [Custom_Attribute]!


        init(dictionary: [String:Any]) {

            custom_Attr = [Custom_Attribute]()
            if let custom_attributesArray = dictionary["custom_attributes"] as? [[String:Any]]{
                for dic in custom_attributesArray{
                    let value = Custom_Attribute(dictionary: dic)
                    custom_Attr.append(value)
                }
            }

        }

        func toDictionary() -> [String:Any]
        {
            var dictionary = [String:Any]()

            if custom_Attr != nil{
                var dictionaryElements = [[String:Any]]()
                for customElement in custom_Attr {
                    dictionaryElements.append(customElement.toDictionary())
                }
                dictionary["custom_attributes"] = dictionaryElements
            }

            return dictionary
        }
    }


    class Custom_Attribute : NSObject{

        var key: String?
        var value: String?

        init(dictionary: [String: Any]) {
            self.key = dictionary["key"] as? String
            self.value = dictionary["value"] as? String
        }

        func toDictionary() -> [String:Any]
        {
            var dictionary = [String:Any]()
            if key != nil{
                dictionary["attribute_code"] = key
            }
            if value != nil{
                dictionary["value"] = value
            }
            return dictionary
        }
    }
}


but its giving me error that "Cannot convert value of type 'NSArray' to type '[String: Any]' in coercion"

Here is an easy solution using ObjectMapper Library, You can refer this class

// Using ObjectMapper Library

import ObjectMapper
class jsonModel : Mappable {
var id = 0
var products: productsDataModel?
required init?(map: Map) {
    //
}
func mapping(map: Map) {
     id <- map["id"]
     products <- map["products"]
}  } class productsDataModel: Mappable {

var items : [itemsDataModel]?

required init?(map: Map) {
    //
}

func mapping(map: Map) {
    items <- map["items"]
}}class itemsDataModel: Mappable {

var status = 0
var custom_attributes = [custom_attributesDataModel]?

required init?(map: Map) {
    //
}

func mapping(map: Map) {
    status <- map["status"]
    custom_attributes <- map["custom_attributes"]
}}class custom_attributesDataModel: Mappable {

var attribute_code = ""
var value = ""

required init?(map: Map) {
    //
}

func mapping(map: Map) {
    attribute_code <- map["attribute_code"]
    value <- map["value"]
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM