JSON键与前导"@"的映射带有restkit [英] mapping of JSON keys with leading "@" with restkit

查看:105
本文介绍了JSON键与前导"@"的映射带有restkit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在为某些项目使用Rest Kit 0.20.3,没有任何问题. 现在,我面临一个问题,即JSON密钥的前导"@"会导致Restkit在映射数据时崩溃.

I am working with rest kit 0.20.3 for some project now without issues. Right now I am facing the problem that the JSON keys have leading "@" which causes Restkit to crash while mapping the data.

JSON如下:

{
    "city": {
        "@name": "Charles Redirection City",
        "@nameUrl": "Charles Redirection City",
        "lat": "52.5238",
        "long": "13.4119",
        "zipCode": "666"
    },
    "categories": {
        "@count": "20037",
        "category": [
            {
                "@count": "2326",
                "@hasChildren": "true",
                "id": "15777",
                "name": "Sticky-Categorie"
            }
        ]
    },
    "additional": {
        "dataKey": [
            {
                "@key": "log-start",
                "$": "home"
            }
        ]
    }
}

使用

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFDictionary 0x95b5360> valueForUndefinedKey:]: this class is not key value coding-compliant for the key hasChildren.'

,因为它尝试在字典上调用hasChildren.我的映射如下所示:

as it tries to call hasChildren on the dictionary. My mapping look like this:

[mapping addAttributeMappingsFromDictionary:@{
                                              @"id"                 :   @"categoryId",
                                              @"name"               :   @"name",
                                              @"@hasChildren"       :   @"doesHaveChildren",
                                              @"@count"             :   @"numbersOfJobs",
                                              @"trackingName"       :   @"trackingString"
                                              }];

我已经尝试应用自己的映射,但不幸的是,该映射无法正常工作. RestKit不支持前导@吗?我已经使用NSJSONSerialization解析了JSON,该方法可以正常工作,但是我真的很喜欢RestKit的映射功能,并希望避免编写自己的映射:)

I already tried to apply my own mapping which unfortunately didn't work. Is a leading @ not supported in RestKit? I already parsed the JSON with NSJSONSerialization which worked fine but I really love the mapping feature of RestKit and would like to avoid writing my own mapping :)

推荐答案

@用于

集合运算符是专用的键路径,这些键路径作为参数传递给valueForKeyPath:方法.运算符由一个以at符号(@)开头的字符串指定.

Collection operators are specialized key paths that are passed as the parameter to the valueForKeyPath: method. The operator is specified by a string preceded by an at sign (@).

我不知道一种告诉NSDictionary忽略它的方法.您可以在服务器端,或者在收到@并将其交给RestKit的映射器之前,将它从JSON中删除.

I don't know of a way to tell NSDictionary to ignore it. You could remove the @ from your JSON, either on the server end, or after you receive it but before you hand it to RestKit's mapper.

这篇关于JSON键与前导"@"的映射带有restkit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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