【问题标题】:Response descriptor without root Json element没有根 Json 元素的响应描述符
【发布时间】:2016-03-09 04:13:20
【问题描述】:
{
    "id": 76,
    "name": "Tom Timberley",
    "gender": 1,
    "follower_count": 1,
    "following_count": 1,
    "me_following": [{
        "id": 5,
        "status": "ACCEPTED",
        "following_me": {
            "id": 2,
            "status": "ACCEPTED"
        }
    }]
}

没有外部 Json 对象。当我做RKRelationshipMapping 时,有什么方法可以从对象following_me 访问根对象?

我有表Following_me 和User,我想将作为用户数据的Json 的根映射到Following_me。

[following_me_mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"@root" toKeyPath:@"user" withMapping:userMapping]];

这里following_me_mapping 是Following_me 表的映射,userMapping 是User 表的映射。

但指定 @root 不起作用。

[RKResponseDescriptor responseDescriptorWithMapping:mapping
                                             method:RKRequestMethodGET
                                        pathPattern:nil
                                            keyPath:nil
                                    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
 ];

这就是我添加响应描述符的方式。

【问题讨论】:

    标签: ios json restkit


    【解决方案1】:

    您似乎在向后考虑问题,并对您的响应描述符正在处理的内容感到困惑。

    通过设置pathPattern:nil keyPath:nil,您的响应描述符将对您发出的任何请求处于活动状态(它不会被路径模式过滤)并且将从根处理 JSON 有效负载(没有导航的关键路径到)。

    因此,您的初始映射已经应该处理根。然后为 me_following 键添加关系以处理该内容。在该映射中,您为following_me 添加关系。

    通过这种方式,您可以在进行过程中向下钻取映射它的数据,而不是您的描述说您想从一开始就钻入然后尝试再次导航回来(这需要一种完全不同且更复杂的方法)。

    【讨论】:

      【解决方案2】:

      也许你可以将“MJExtension.Framework”用于“json to model”数据(https://github.com/CoderMJLee/MJExtension),将这些数据显示到模型而不是 json 数据(字典)上可以简化你的问题,祝你好运!~~

      【讨论】:

      • 是的,但是我正在使用 Restkit 映射来编写对核心数据的 Json 响应,如果我可以使用 Restkit 本身来做到这一点,那就太好了。
      • 好的,我看到了这个 Restkit 映射,它实际上是多用途的,如果你有大部分时间,你可以研究它。但对我来说,我需要为项目做一些捷径。你能给我一些这个工具包的链接或资源?第三季~
      • 他们的文档本身是最好的起点,而且它在 Core-data Restkit 之上工作
      【解决方案3】:
      yes you can save the array of dictionary.allkeys, then put it in the for loop.
      
      
      let arrayOFKeys = dictionary.allKeys
      for(int i=0;i<arrayOFKeys.count;i++)
      {
       dictionary.valueforKey(arrayOFKeys.objectAtIndex[i]) // here you will get value and key(root) as well
      //key = arrayOFKeys.objectAtIndex[i]
      //value = dictionary.valueforKey(arrayOFKeys.objectAtIndex[i])
      
      }
      

      【讨论】:

      • 好的,但是我如何使用 RKRelationshipmapping 来实现这一点
      • 哦,对不起,我从未使用过 RKRelationshipmapping
      猜你喜欢
      • 2015-01-18
      • 2020-10-05
      • 2014-06-04
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多