我正在尝试在我的一个iPhone App中使用最新的RestKit。
我得到的JSON字符串是
{ “testdata”:1374226642, “errorCode”:null, “errorMessage”:null, “mycontent”:{ “myid”:“1”, “名字”:“mrbala”, “textmessage”:[ “text1231” “text1232” “text1234” ] “picturemessage”:[ “text1231” “text1232” “text1234” ] “mydate”:“0”, “你的日子”:“0” }}
我的代码如下。
@interface TextMessage : NSObject
@property (nonatomic,strong)NSString *textmessage;
@end
PictureMessage
@interface PictureMessage : NSObject
@property (nonatomic,copy)NSString *picturemessage;
@end
我的内容
@interface MyContent : NSObject
@property (nonatomic,copy)NSString *myid;
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSArray *textMessage;
@property (nonatomic,copy)NSArray *pictureMessage;
@property (nonatomic,copy)NSString *mydate;
@property (nonatomic,copy)NSString *yourdate;
@end
MainItem
@interface WeatherService : NSObject
@property (nonatomic,copy) NSDate *testData;
@property (nonatomic,copy) NSString *errorCode;
@property (nonatomic,copy) NSString *errorMessage;
@property (nonatomic,strong)MyContent *mycontent;
@end
映射是
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];
RKObjectMapping *textmessage = [RKObjectMapping mappingForClass:[TextMessage class]];
[textmessage addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"textmessage"]];
RKObjectMapping *picturemessage = [RKObjectMapping mappingForClass:[PictureMessage class]];
[picturemessage addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"picturemessage"]];
RKObjectMapping *mycontent = [RKObjectMapping mappingForClass:[MyContent class]];
[mycontent addAttributeMappingsFromDictionary:@{@"myid": @"myid",
@"name" : @"name",
@"mydate":@"mydate",
@"yourdate":@"yourdate"}];
[mycontent addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"textmessage" toKeyPath:@"textmessage" withMapping:textmessage]];
[mycontent addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"picturemessage" toKeyPath:@"picturemessage" withMapping:picturemessage]];
RKObjectMapping *mainitem = [RKObjectMapping mappingForClass:[MainItem class]];
[mainitem addAttributeMappingsFromDictionary:@{
@"testdata" : @"requestDate",
@"errorCode" : @"errorCode",
@"errorMessage" : @"errorMessage"}];
[mainitem addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"newsContent" toKeyPath:@"newsContent" withMapping:mycontent]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mainitem method:RKRequestMethodAny pathPattern:nil keyPath:@"" statusCodes:statusCodes];
NSString *lURL = @"http://xxx.xxx.143.139/test.json";
NSString *lModified = [[lURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *URL = [NSURL URLWithString:lModified];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
RKLogInfo(@"Load Collection of Articles : %@",mappingResult.array);
NSMutableArray *lArray =[[NSMutableArray alloc]initWithArray:mappingResult.array];
for (int i=0; i < [mappingResult.array count]; i++) {
MainItem *lArticle = [lArray objectAtIndex:i];
for (int l=0;l < lArticle.newsContent.textmessage.count; l++){
TextMessage *lMessage = [lArticle.newsContent.textmessage objectAtIndex:l];
NSLog(@"Message :: %@",[lMessage textmessage]);
}
for (int l=0;l < lArticle.newsContent.picturemessage.count; l++){
PictureMessage *lMessage = [lArticle.newsContent.picturemessage objectAtIndex:l];
NSLog(@"Picture :: %@",[lMessage picturemessage]);
}
NSLog(@"Article :: %@",[lArticle requestDate]);
}
}failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation Failed :: %@",error);
}];
[objectRequestOperation start];
一切正常。只有当我尝试打印消息文本时,它才会打印完整的映射信息。行NSLog(@“Message ::%@”,[lMessage textmessage]);打印完整的映射信息。我不能简单地访问textMessage。这就是输出。
Message :: text1231 ({
HTTP = {
request = {
URL = "http://xxx.xxx-xxx.xx/mycontent/?id=1";
headers = {
};
method = GET;
};
response = {
URL = "http://xxx.xxx-xxx.xx/mycontent/?id=1";
headers = {
Connection = "Keep-Alive";
"Content-Type" = "text/html; charset=UTF-8";
Date = "Fri, 19 Jul 2013 11:12:16 GMT";
Server = "Apache/2.2.22";
"Set-Cookie" = "fe_typo_user=9862282e43cabf58a4226fe92338877b; path=/";
"Transfer-Encoding" = Identity;
"X-Pad" = "avoid browser bug";
"X-Powered-By" = "PHP/5.3.19";
};
};
};
mapping = {
collectionIndex = 2;
rootKeyPath = "";
};
})
这有什么遗漏吗?
此致
Nirav