-
Notifications
You must be signed in to change notification settings - Fork 2
Collection Types
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
{
"business_uuid" : 9223123456754776000,
"business_name" : "NYC Restaurant",
"business_ratings" : [ 5, 4, 5, 4 ],
"business_location" : {
"longitude" : 40.7053319,
"latitude" : -74.0129945
},
"business_open" : 1
}
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.
A Good Day Production
LinkedIn | Twitter @AntonTheDev