aryaxt / OCMapper

Objective-C library to easily map NSDictionary to model objects, works perfectly with Alamofire. ObjectMapper works similar to GSON
MIT License
347 stars 45 forks source link

Nested JSON #58

Open terasupernova opened 8 years ago

terasupernova commented 8 years ago

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.