Как отобразить вложенный массив объектов с помощью Swift Object Mapper? - PullRequest
0 голосов
/ 12 апреля 2019

Я пытаюсь отобразить массив объектов с помощью 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"
            }
        ]
    },
.
.
.

]

1 Ответ

1 голос
/ 12 апреля 2019

Если вы все равно хотите отправить завершениеHandler, используйте код ниже:

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

}
...