RestKit的动态映射会解决这个复杂的JSON映射吗? [英] Will RestKit's dynamic mapping solve this complex JSON mapping?

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

问题描述

我在我的应用程序中使用RestKit,它需要使用以这种方式构造传入数据的现有同步服务:

I am using RestKit in my app, which needs to use an existing synchronization service that structures the incoming data this way:

{
  "timestamp": 000000000001,
  "status" : 0,
  "syncData":[
    {
      "errors":[],
      "rows":[ {"name":"AAA", ...},
               {"name":"BBB", ...}, 
               ...],
      "rtype":"FOO" },
    {
      "errors":[],
      "rows":[ {"id":1, "description":"ZZZ", ....},
               {"id":2, "description":"YYY", ....}, 
               ...],
      "rtype":"BAR"
    }, ...

我是RestKit的新手并试图弄明白解决此问题的最佳方法,以及将相同结构的数据发送回服务器的补充问题。我正在使用带有RestKit的Core Data。

I'm new to RestKit and trying to figure out the best way to solve this problem, and the complementary problem of sending this same structure of data back to the server. I'm using Core Data with RestKit.

我已经映射了一个SyncResponse实体来保存顶级数据,我想要从中得到的是一个集合FOO对象,AAA,BBB等,以及BAR对象的集合,ZZZ,YYY等,以及几十个其他类的对象集合,其类别由rtype表示字段。

I've mapped a SyncResponse entity to hold the top level data, and what I want to get out of this is a collection of FOO objects, "AAA", "BBB", etc., and a collection of BAR objects, "ZZZ", "YYY", etc., and a few dozen other collections of objects whose Class is indicated by the "rtype" field.

我在这里阅读了关于动态映射的文档部分以及一些示例代码和帖子,但我不知道动态映射在这种情况下是如何工作的不属于 {a:{是A},b:{is B}} 格式。这是否可以使用动态映射,如果是这样,我在这里缺少什么概念?

I've read the doc section on dynamic mapping and some example code and postings here, but I don't see how dynamic mapping works in this case as it is not of the {"a":{is A}, "b":{is B}} format. Is this possible using dynamic mapping, and if so, what concepts am I missing here?

假设有可能,我如何从FOO和BAR的集合开始发回数据,当然用SyncUpdateRequest包装器替换SyncResponse?

Assuming it is possible, how do I, starting with collections of FOOs and BARs send data back, of course replacing the SyncResponse with something like a SyncUpdateRequest wrapper?

推荐答案

我认为你不能单独使用一组映射来做到这一点。

I don't think you'll be able to do this using a set of mappings alone.

您最好的选择可能是为每个项目创建映射,为整个结构创建一个映射。整个映射只是将数组提取为字典的 NSArray 。一旦你有了数组,你可以自己迭代它,检查类型,然后应用 RKMapperOperation 来执行映射。

Your best option may be to create your mappings for each item and one for the overall structure. The overall mapping just extracts the array as an NSArray of dictionaries. Once you have the array you can iterate over it yourself, check the type and then apply an RKMapperOperation to perform the mappings.

为了发送您的更新请求,我将其视为一个完全独立的事情。我将构建一个字典数组,其中字典具有某些信息的普通键/值对以及的复杂键/值对。然后,您的请求映射就是这个字典数组(包括自定义部分)和行(应该是类的响应映射的反转)。然后,RestKit应该能够以标准方式处理它(与上面的响应映射的复杂性相比)。

For sending your update request, I'd look at it as a quite separate thing. I'd build an array of dictionaries where the dictionaries have 'plain' key / value pairs for some information and 'complex' key / value pairs for the rows. Your request mapping is then in terms of this array of dictionaries (which cover the custom parts) and the rows (which should be the inverse of your response mapping for the class). Then RestKit should be able to handle it in the standard way (compared to the complexity of your response mapping above).

这篇关于RestKit的动态映射会解决这个复杂的JSON映射吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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