我有映射简单JSON响应的工作部分。下面是它的样子:
RKObjectMapping *eventMapping = [Event responseMapping];
RKResponseDescriptor *listEventsResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:eventMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:listEventsResponseDescriptor];
当然还有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
因此,这种方式允许我处理下一个响应(服务器响应底部的处理字段):
(
{
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";
},
{ ...
关于日期间隔的大部分服务器响应。这个JSON在字段JodaTime Interval上创建了Java Spring。
问题:我如何从这个JSON中获得两个NSDate属性字段?
创建一个与JSON结构匹配的类MyRestKitJodaTimeModel
(或多个类),您可以为此使用RestKit。然后使用数据(您可以使用毫秒)并从中创建一个NSDate
。
NSDate datefromJSON = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[myRestKitJodaTimeModel millis]/1000]];
与MyRestKitJodaTimeModel myRestKitJodaTimeModel
包含数据从你的JSON。
这假设您有很好的理由使用RestKit,例如许多其他类匹配得很好。另一方面,如果这是您接收到的唯一数据,那么使用RestKit将使您的生活变得更加困难而不是容易。