RestKit动态嵌套映射 [英] RestKit Dynamic nested mapping

查看:103
本文介绍了RestKit动态嵌套映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到restkit文档是相当不错,有各种各样的对象建模的例子。还有一个嵌套映射的例子,但我发现我的场景有点不同。 RestKit文档提供了嵌套属性与以下json格式的示例映射。



RestKit文档中的示例JSON结构

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

假设我的json有点不同, / p>

我的JSON结构

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

}
}

我创建了两个不同的managedobject模型,



我的结构有两个不同的实体产品和创建者映射上述JSON对象。

 产品创建者

标识符< ------------------->> name
author email
favouriteAnimal

现在,我的映射看起来像这样产品模型是;



这是我如何映射Product实体,
[mapping mapKeyPath:idtoAttribute:identifier];
[mapping mapKeyPath:authortoAttribute:author];



但是请注意,映射嵌套字典属性对我来说不起作用。
// [mapping mapKeyOfNestedDictionaryToAttribute:@creators];



现在,在authors类中。



我无法找出映射上述JSON结构的常用方法。

解决方案

如果您可以控制Web服务,我强烈建议您重新组织您的响应数据,如下所示:

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

按照这种结构,你可以使用RestKit的嵌套映射特性,正确反映在由对象加载器委托接收的反序列化对象中。 RestKit依赖于命名和结构标准来简化实现任务所需的代码。您的示例偏离了键值编码标准,因此RK不提供与您的数据格式交互的简单方法。



如果您没有访问权限或您无法更改它,我想您将需要映射已知的键值对与映射和执行剩余的分配与自定义评估程序。您需要假设未知键实际上是关联创建者的名称值,其关联值包含每个属性的属性哈希值。使用它,您将手动重建每个对象。


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.

解决方案

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.

这篇关于RestKit动态嵌套映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆