Anton
Anton

Reputation: 3120

RestKit JSON Mapping

I have given JSON and cannot parse partial data. It seems dictionary into dictionary:

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

I am using the latest version of RESTKit but I cannot properly parse under discounts.

my RestKit settings are:

responseMapping.addAttributeMappingsFromDictionary([

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

but all values below discounts always return Nil. What I am doing wrong?

Upvotes: 0

Views: 362

Answers (1)

Wain
Wain

Reputation: 119021

You can't directly map using discounts.XXX because discounts is an array and you have no way to index into that array and extract a single value.

You either need to change the source JSON to compress the values out of the dictionary, or create a custom object that you can map each item in the discounts array to.

Technically you could map the whole discounts array, which would give you an array of dictionaries, that you could then unpack in the setter method, but the array of custom objects is usually a better approach.

Upvotes: 1

Related Questions