I have working part for mapping simple JSON response. Here how it look like:
RKObjectMapping *eventMapping = [Event responseMapping];
RKResponseDescriptor *listEventsResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:eventMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:listEventsResponseDescriptor];
And of course Event class (DTO)
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
@interface Event : NSObject
@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *detail;
@property (nonatomic, strong) NSString *image;
+ (RKObjectMapping *) responseMapping;
@end
@implementation Event
+ (NSDictionary *) elementToPropertyMappings {
return @{
@"id": @"id",
@"title": @"title",
@"detail": @"detail",
@"image": @"image",
};
}
+ (RKObjectMapping *) responseMapping {
// Create an object mapping.
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Event class]];
[mapping addAttributeMappingsFromDictionary:[Event elementToPropertyMappings]];
return mapping;
}
@end
So, this way allows me working with next response (processed field in the bottom of the server response):
(
{
dateInterval = {
afterNow = 0;
beforeNow = 1;
chronology = {
zone = {
fixed = 0;
id = "Europe/Kiev";
uncachedZone = {
cachable = 1;
fixed = 0;
id = "Europe/Kiev";
};
};
};
end = {
afterNow = 0;
beforeNow = 1;
centuryOfEra = 20;
chronology = {
zone = {
fixed = 0;
id = "Europe/Kiev";
uncachedZone = {
cachable = 1;
fixed = 0;
id = "Europe/Kiev";
};
};
};
dayOfMonth = 12;
dayOfWeek = 4;
dayOfYear = 71;
equalNow = 0;
era = 1;
hourOfDay = 13;
millis = 1426160220000;
millisOfDay = 49020000;
millisOfSecond = 0;
minuteOfDay = 817;
minuteOfHour = 37;
monthOfYear = 3;
secondOfDay = 49020;
secondOfMinute = 0;
weekOfWeekyear = 11;
weekyear = 2015;
year = 2015;
yearOfCentury = 15;
yearOfEra = 2015;
zone = {
fixed = 0;
id = "Europe/Kiev";
uncachedZone = {
cachable = 1;
fixed = 0;
id = "Europe/Kiev";
};
};
};
endMillis = 1426160220000;
start = {
afterNow = 0;
beforeNow = 1;
centuryOfEra = 20;
chronology = {
zone = {
fixed = 0;
id = "Europe/Kiev";
uncachedZone = {
cachable = 1;
fixed = 0;
id = "Europe/Kiev";
};
};
};
dayOfMonth = 12;
dayOfWeek = 4;
dayOfYear = 71;
equalNow = 0;
era = 1;
hourOfDay = 13;
millis = 1426160160000;
millisOfDay = 48960000;
millisOfSecond = 0;
minuteOfDay = 816;
minuteOfHour = 36;
monthOfYear = 3;
secondOfDay = 48960;
secondOfMinute = 0;
weekOfWeekyear = 11;
weekyear = 2015;
year = 2015;
yearOfCentury = 15;
yearOfEra = 2015;
zone = {
fixed = 0;
id = "Europe/Kiev";
uncachedZone = {
cachable = 1;
fixed = 0;
id = "Europe/Kiev";
};
};
};
startMillis = 1426160160000;
};
detail = "\"I know you cannot count beyond ten, so I will tell you. Hold up your two hands. On both of them you have altogether ten fingers and thumbs. Very well. I now take this grain of sand—you hold it, Hoo-Hoo.\" He dropped the grain of sand into the lad's";
id = 14;
image = "6f655fd8-ac0d-454f-8b8d-5c93eab34030.png";
new = 0;
title = "I know you";
},
{ ...
Most of the server's response regarding date interval. This JSON created Java Spring, on the field JodaTime Interval.
Question: How can I get two NSDate property fields from this JSON?
Create a class MyRestKitJodaTimeModel
(or a number of classes) that matches the JSON structure and you can use RestKit for it. Then use the data (you could use the milliseconds) and create an NSDate
from it.
NSDate datefromJSON = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[myRestKitJodaTimeModel millis]/1000]];
With MyRestKitJodaTimeModel myRestKitJodaTimeModel
containing that data from your JSON.
This assumes that you have good reasons for using RestKit, such as lots of other classes that match well. On the other hand, If is is the only data that you receive than using RestKit would make your life more difficult than easier.