I've got the following JSON coming from my webservice:
"GasPrices":{
"Ai92":{
"Price":30.1000,
"LastDate":"\/Date(1385337600000)\/",
"Votes":0
},
"Ai95":{
"Price":33.2000,
"LastDate":"\/Date(1385337600000)\/",
"Votes":0
}
I want to map it to NSDictionary
, whose keys would be NSString
s and values would be of my custom class, say, PriceInfo
.
What I got now with default setup is NSDictionary whose values are also NSDictionaries.
How can I achieve the desired mapping?
UPD. Here's my full setup for now.
@interface FillingStation : NSObject
@property (nonatomic, strong) NSNumber *uid;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSDictionary *fuelPrices;
@end
@interface PriceInfo : NSObject
@property (nonatomic, strong) NSNumber *price;
@property (nonatomic, strong) NSDate *lastDate;
@property (nonatomic, strong) NSNumber *votes;
@end
Configuring the mapping:
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[FillingStation class]];
[mapping addAttributeMappingsFromDictionary:@{
@"Id" : @"uid",
@"Title" : @"title",
@"GasPrices" : @"fuelPrices"
}];
This results in fuelPrices
is NSDictionary
with the structure:
NSString -> NSDictionary,
NSString -> NSDictionary,
...
And I want it to be:
NSString -> PriceInfo,
NSString -> PriceInfo,
...
And I don't want an intermediate dictionary in FillingStation
which I can then manually map.
Okay, seems like I found the answer. Not exactly what I wanted, yet acceptable for me: https://github.com/RestKit/RestKit/wiki/Object-Mapping#handling-dynamic-nesting-attributes
I had to add another property to
PriceInfo
class and changefuelPrices
fromNSDictionary
toNSSet
(NSArray
would also work, but I don't need ordering), so it became:and my mapping now looks like this: