org.joda.time.Interval 对象的 RestKit 映射
RestKit mapping for org.joda.time.Interval obj
我有映射简单 JSON 响应的工作部分。这是它的样子:
RKObjectMapping *eventMapping = [Event responseMapping];
RKResponseDescriptor *listEventsResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:eventMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:listEventsResponseDescriptor];
当然还有事件 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属性 字段?
创建一个 class MyRestKitJodaTimeModel
(或多个 classes)匹配 JSON 结构,你可以为它使用 RestKit。然后使用数据(您可以使用毫秒)并从中创建一个 NSDate
。
NSDate datefromJSON = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[myRestKitJodaTimeModel millis]/1000]];
MyRestKitJodaTimeModel myRestKitJodaTimeModel
包含来自您的 JSON 的数据。
这假设您有充分的理由使用 RestKit,例如许多其他 classes 匹配得很好。另一方面,如果这是你收到的唯一数据,那么使用 RestKit 会让你的生活变得更加困难而不是容易。
我有映射简单 JSON 响应的工作部分。这是它的样子:
RKObjectMapping *eventMapping = [Event responseMapping];
RKResponseDescriptor *listEventsResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:eventMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:listEventsResponseDescriptor];
当然还有事件 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属性 字段?
创建一个 class MyRestKitJodaTimeModel
(或多个 classes)匹配 JSON 结构,你可以为它使用 RestKit。然后使用数据(您可以使用毫秒)并从中创建一个 NSDate
。
NSDate datefromJSON = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[myRestKitJodaTimeModel millis]/1000]];
MyRestKitJodaTimeModel myRestKitJodaTimeModel
包含来自您的 JSON 的数据。
这假设您有充分的理由使用 RestKit,例如许多其他 classes 匹配得很好。另一方面,如果这是你收到的唯一数据,那么使用 RestKit 会让你的生活变得更加困难而不是容易。