如何使用Swift Object Mapper映射嵌套的对象数组?

问题描述 投票:0回答:1

我正在尝试使用Object Mapper映射对象数组

到目前为止我有这个代码,我的映射不成功

do {
    if let data = data,  let sectorData =  Mapper<SubSectorsModel>().mapArrayOfArrays(JSONObject: try JSONSerialization.data(withJSONObject: data, options: [])) {
        completionHandler(sectorData,(response as! HTTPURLResponse), error)
        print("SectionData Received Successfully")
    }
} catch {
    completionHandler(nil,(response as! HTTPURLResponse), error)
    print("Error parsing json get sector data: ", error.localizedDescription)
}

我的Json数据如下:

[
    {
        "SECTOR_NAME": "MANUFACTURERS",
        "ID": "8",
        "SECTOR": [
            {
                "ID": "144",
                "NAME": "Biomass Processing"
            },
            {
                "ID": "8",
                "NAME": "Servicing engines and motors"
            },
            {
                "ID": "23",
                "NAME": "Furniture & fittings"
            },
            {
                "ID": "31",
                "NAME": "Fabrics & textiles"
            },
            {
                "ID": "20",
                "NAME": "Hand and machine tools"
            },
            {
                "ID": "28",
                "NAME": "Safety and security products"
            },
            {
                "ID": "147",
                "NAME": "Jewellery"
            },
            {
                "ID": "156",
                "NAME": "Beverages"
            },
            {
                "ID": "165",
                "NAME": "Stationery"
            },
            {
                "ID": "9",
                "NAME": "Industrial equipment"
            },
            {
                "ID": "25",
                "NAME": "Cleaning equipment"
            },
            {
                "ID": "33",
                "NAME": "Household consumer products"
            },
            {
                "ID": "162",
                "NAME": "Paper Products"
            },
            {
                "ID": "170",
                "NAME": "Memoribilia"
            },
            {
                "ID": "143",
                "NAME": "Food Products"
            },
            {
                "ID": "22",
                "NAME": "Automotive  aviation  marine and rail products"
            },
            {
                "ID": "30",
                "NAME": "Household appliances"
            },
            {
                "ID": "151",
                "NAME": "Iron Sheet"
            },
            {
                "ID": "167",
                "NAME": "Cosmetics"
            },
            {
                "ID": "11",
                "NAME": "Fuel  Lubricants & Detergents"
            },
            {
                "ID": "19",
                "NAME": "Electrical appliances and equipment"
            },
            {
                "ID": "27",
                "NAME": "Packaging products"
            },
            {
                "ID": "7",
                "NAME": "Engines & parts"
            },
            {
                "ID": "24",
                "NAME": "Glass products"
            },
            {
                "ID": "32",
                "NAME": "Clothing & footwear"
            },
            {
                "ID": "152",
                "NAME": "Building Material"
            },
            {
                "ID": "142",
                "NAME": "Food Processing and Packaging"
            },
            {
                "ID": "21",
                "NAME": "Plastic products"
            },
            {
                "ID": "29",
                "NAME": "Pool & garden products"
            },
            {
                "ID": "157",
                "NAME": "Steel Products"
            },
            {
                "ID": "138",
                "NAME": "Optical Prescription Lenses"
            },
            {
                "ID": "10",
                "NAME": "Servicing & refurbishing"
            },
            {
                "ID": "18",
                "NAME": "Chemical"
            },
            {
                "ID": "26",
                "NAME": "Board  paper and"
            }
        ]
    },
.
.
.

]
json swift objectmapper
1个回答
1
投票

如果您只想发送completionHandler,请使用以下代码:

struct MyJsonStruct : Decodable {
    struct SectorStruct : Decodable {
        var ID : String
        var NAME : String
    }
    var SECTOR_NAME : String
    var ID : String
    var SECTOR : [SectorStruct]
}


func handle(_ data : Data ) {
    do {
        let sectorData = try JSONDecoder().decode(MyJsonStruct.self, from: data) as MyJsonStruct

        let yourArray = sectorData.SECTOR // if you need an array result

        completionHandler(sectorData,(response as! HTTPURLResponse), error)
        print("SectionData Received Successfully")
    } catch {
        completionHandler(nil,(response as! HTTPURLResponse), error)
        print("Error parsing json get sector data: ", error.localizedDescription)
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.