RestKit JSON 映射

RestKit JSON Mapping

我已经给出了 JSON 并且无法解析部分数据。好像字典进字典了:

{
    "products": [
        {
            "id": 6796,
            "title": "my title",
            "description": "desc",
            "code": "12345",
            "valueType": "null",
            "discounts": [
                {
                    "minPrice": null,
                    "maxPrice": null,
                    "value": 20,
                    "avail": false
                }
            ]
        }
    ]
}

我正在使用最新版本的 RESTKit 但我无法在 discounts 下正确解析。

我的 RestKit 设置是:

responseMapping.addAttributeMappingsFromDictionary([

            "id"                        : "id",
            "code"                      : "code",
            "title"                     : "title",
            "valueType"                 : "valueType",
            "description"               : "desc",
            "discounts.minPrice"        : "minPrice",
            "discounts.maxPrice"        : "maxPrice",
            "discounts.value"           : "value",
            "discounts.avail"    : "avail",
            ])

但所有低于折扣的值始终 return 无。我做错了什么?

您不能使用 discounts.XXX 直接映射,因为 discounts 是一个数组,您无法索引到该数组并提取单个值。

您需要更改源 JSON 以压缩字典中的值,或者创建一个自定义对象,您可以将 discounts 数组中的每个项目映射到。

从技术上讲,您可以映射整个 discounts 数组,这将为您提供一个字典数组,然后您可以在 setter 方法中将其解包,但自定义对象数组通常是一个更好的方法。