2

I am trying to map an array of objects using Object Mapper

I have this code so far, and my mapping is not successful

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

My Json data is as follows:

[
    {
        "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"
            }
        ]
    },
.
.
.

]
3
  • Does it make a sence solving with structs? Commented Apr 12, 2019 at 8:29
  • 1
    Can you add your SubSectorsModel in the question? Commented Apr 12, 2019 at 9:42
  • If my answer is correct, check it plz. Commented Apr 12, 2019 at 10:51

1 Answer 1

1

If you just want to send completionHandler anyway, use code below:

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.