RestKit Dynamic nested mapping

半世苍凉 提交于 2019-12-24 05:16:06

问题


I see that the restkit document is quite nice and has variety of examples on object modelling. There is also an example of nested mapping but I find my scenario little bit different than this. RestKit documentation provides the example mapping of the nested attribute with the following json format.

Sample JSON structure from the RestKit Documentation :

{
  "blake": {        
    "email": "blake@restkit.org",        
    "favorite_animal": "Monkey"    
  },    
  "sarah": {
    "email": "sarah@restkit.org",   
    "favorite_animal": "Cat"
  }
}

Suppose that my json is a bit different as this;

My JSON structure :

{ 
  "id" : 1,
  "author" : "RestKit",
  "blake": {        
    "email": "blake@restkit.org",        
    "favorite_animal": "Monkey"    
  },    
  "sarah": {
    "email": "sarah@restkit.org",   
    "favorite_animal": "Cat"
  }
}

I created two different managedobject model with the following attributes and to many relations.

Two different entities for my structure Product and creator to map the above JSON object.

Product                                           Creator 

identifier          <------------------- >>       name
author                                            email
                                                  favouriteAnimal

Now, my mapping would look like this for Product model would be;

This is how I map the Product entity, [mapping mapKeyPath:"id" toAttribute:"identifier"]; [mapping mapKeyPath:"author" toAttribute: "author"];

But note here, mapping the nested dictionary attribute does not work for me. // [mapping mapKeyOfNestedDictionaryToAttribute:@"creators"];

Now, in the authors class.

I could not figure out the usual way to map the above JSON structure.


回答1:


If you have control over the web service, I would strongly recommend reorganizing your response data like this:

{
  product: 
  {
    id: 1,
    author: 'RestKit',
    creators: [
      {
        id: 1,
        name: 'Blake',
        email: '...',
        favorite_animal: 'Monkey'
      },
      {
        id: 2,
        name: 'Sarah',
        email: '...',
        favorite_animal: 'Cat'
      }
    ]
  }
}

Following this structure, you'd be able to use RestKit's nested mapping features, and the relationship would be correctly reflected in the deserialized objects received by the object loader delegate. RestKit relies on naming and structure standards to simplify the code required to achieve the task. Your example deviates from key-value coding standards, so RK doesn't provide an easy way to interact with your data format.

If you don't have access or you can't change it, I think you'll need to map known key-value pairs with a mapping and perform the remaining assignments with a custom evaluator. You'd need to assume the unknown keys are actually name values for associated creators and their associated values contain the attribute hash for each. Using that, you'd then reconstruct each object manually.



来源:https://stackoverflow.com/questions/11912731/restkit-dynamic-nested-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!