Skip to content

Nested JSON #58

@terasupernova

Description

@terasupernova

Hi, i have a JSON like this

{
  "total_item": 250,
  "item_per_page": 50,
  "current_page": 1,
  "total_page": 5,
  "data": [
    {
      "data": {
        "id": 1,
        "name": "Happy New Year 2016",
        "logo": {
          "ratio": 1,
          "path_1x": "https://3.bp.blogspot.com/-KUivQF0u7ME/VoMhw9LmM4I/AAAAAAAAASM/wt4gB6bT-Mk/s640/happy-new-year-2016-wishes.jpg",
          "path_2x": "https://3.bp.blogspot.com/-KUivQF0u7ME/VoMhw9LmM4I/AAAAAAAAASM/wt4gB6bT-Mk/s640/happy-new-year-2016-wishes.jpg"
        },
        "main_category": {
          "id": 0,
          "name": "Mall"
        }
      },
      "type": 2
    },

Inside "data" got another "data". My model class is

    var totalPage: NSNumber!
    var totalItem: NSNumber!
    var currentPage: NSNumber!
    var itemPerPage: NSNumber!
    var data: [T]!

What i going to do is i need to get the data.data from JSON and map to data.
So i write a code

class func createFromJSON<T>(type: T, dictionary:Dictionary<String, AnyObject>, provider: InCodeMappingProvider) -> PaginateListModel<T>
    {
        var model = PaginateListModel<T>()
        model = ObjectMapper.sharedInstance().objectFromSource(dictionary, toInstanceOfClass: PaginateListModel<T>.self) as! PaginateListModel<T>

        let dictData = dictionary["data"] as! [Dictionary<String, AnyObject>]
        provider.mapFromDictionaryKey("data.data", toPropertyKey: "data", withObjectType:  T.self , forClass: PaginateListModel<T>.self)
        ObjectMapper.sharedInstance().mappingProvider = provider
        model.data = ObjectMapper.sharedInstance().objectFromSource(dictData, toInstanceOfClass: T.self) as! [T]


        return model
    }

But i can't get the result. Please help. Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions