0

My response is :

  [
  {
    "menu_code" : "NDS",
    "items" : [
      {
        "unit" : "Nos",
        "name" : "Chapathi\/Pulkas",
        "quantity" : 2
      },
      {
        "unit" : "Cup",
        "name" : "Palya\/Curry",
        "quantity" : 1
      }
    ],
    "is_active" : 1,
    "image" : "nds.jpg",
    "menu_name" : "Normal Diet South"
  },
  {
    "menu_code" : "NCCD",
    "items" : [
      {
        "menu_code" : "NDS",
        "name" : "Monday"
      },
      {
        "menu_code" : "NDN",
        "name" : "Tuesday"
      }
    ],
    "is_active" : 1,
    "image" : "NCCD.jpg",
    "menu_name" : "Normal Combo Corporate Diet"
  }
]

Today 2 format i have .In this both format only my data will come from response.And i need to show them in collection view.

My api call :

func getAllCatogory(){

        TransportManager.sharedInstance.AllCatogory { (dt, err) in

            if let _ = err{

            }else{
                if let data = dt as? String {
                   let pro = Mapper<AllCatagories>().map(JSONString: data)
                print(data) // getting data
                print(pro as Any) // getting nil

                }

            }
        }
    }

My model :

class AllCatagories: Mappable{

    var menu_code = ""
    var items: Array<AllCatProducts> = []
    var is_active = 0
    var image = ""
    var menu_name = ""

    required init?(map: Map) {

    }

    init() {

    }

    func mapping(map: Map) {

        menu_name <- map["menu_name"]
        is_active <- map["is_active"]
        menu_code <- map["menu_code"]
        image <- map["image"]
       items <- map["items"]
    }
}

Below i have created one more model class for the item inside my json.

class AllCatProducts: Mappable{

    var name = ""
    var quantity = 0
    var unit = ""
    var menu_code = ""



    required init?(map: Map) {

    }

    init() {

    }

    func mapping(map: Map) {
        name <- map["name"]
        quantity <- map["quantity"]
        unit <- map["unit"]
        menu_code <- map["menu_code"]
    }
}

The issues is i am getting nil in my pro.Not sure when i am doing wrong.

Thanks

david
  • 636
  • 2
  • 12
  • 29

1 Answers1

-1

You can try

struct AllCatagories: Codable {
    let menuCode: String
    let items: [AllCatProducts]
    let isActive: Int
    let image, menuName: String
}

struct AllCatProducts: Codable {
    let unit: String?
    let name: String
    let quantity: Int?
    let menuCode: String? 
}

do {
   let decoder = JSONDecoder()
   decoder.keyDecodingStrategy = .convertFromSnakeCase
   guard let str = dt as? String else { return }
   let res  = try decoder.decode([AllCatagories].self, from:str.data(using: .utf8)!)
   print(res)
 }
 catch {
  print(error)
 }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/186585/discussion-on-answer-by-sh-khan-how-to-map-the-data-which-has-array-of-data). – Samuel Liew Jan 12 '19 at 07:04
  • @Sh_Khan i tried like thsi ` for data in res { if let name = data.menuName { print(name) } }` not even its going inside name . i wrote this in my `getallcat` method – david Jan 12 '19 at 12:46
  • @Sh_Khan any help on here https://stackoverflow.com/questions/54434614/how-to-save-struct-values-with-codable-decodable – david Jan 30 '19 at 06:44