Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested JSON #58

Open
terasupernova opened this issue Oct 12, 2016 · 0 comments
Open

Nested JSON #58

terasupernova opened this issue Oct 12, 2016 · 0 comments

Comments

@terasupernova
Copy link

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.

@aryaxt aryaxt closed this as completed Oct 13, 2016
@aryaxt aryaxt reopened this Oct 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants