Skip to content

Collection Types

Anton edited this page Jul 16, 2017 · 14 revisions

JSONModelKit's support for mapping Dictionaries and Arrays when parsing a responses' collection values is quite easy. Let's assume in the example below that the business listing result returns an array of ratings, and we would like to store them as an Array type

Response Dictionary

{
    "business_uuid"	: 9223123456754776000,
    "business_name"	: "NYC Restaurant",
    "business_ratings"	: [ 5, 4, 5, 4 ],
    "business_location" : {
        "longitude" : 40.7053319,
        "latitude"  : -74.0129945
    },
    "business_open"     : 1
}

Mapping

First, update a model mapping for the Business object by defining setting the type key definition to Array, and adding a new subtype key to define the subtype as Int.

/Model/Mappings/Business.json | PLIST Equivalent
{
    "uuid" : { ... },
    "businessName" : { ... },
    "ratings" : {
        "key" : "ratings",
        "type" : "Array",
        "subtype" : "Double"
    },
    "metaTags" : { ... },
    "open" : { ... }
}

When parsing the data for a Business object, JSONModelKit will create a parsed ratings Array<Double>, and will assign the resulting value to the ratings property of the Business instance before returning it.

This also works for Dictionaries by setting the type key to Dictionary. When parsing a dictionary, JSONModelKit will use the assigned keys in the response when setting the values in the parsed Dictionary. If the response is an array, and the property to be mapped is a dictionary, JSONModelKit will use an incremental index as the key starting with zero.

Note: When parsing collections, subtype can be any of the native value types String, Int, Double, Float, Bool, and/or any object generated by a JSONModelKit.

Clone this wiki locally
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy