【发布时间】:2013-06-23 10:11:20
【问题描述】:
我有一个 JSON 文件,我从网上下载并使用 RestKit 进行解析。我的问题是,我有一个包含字符串数组的属性,我试图将其映射到他们自己的对象中。出于某种原因,在解析该字符串时,它最终会在字符串中包含元数据对象的描述。请参阅下面的代码 sn-ps 了解我目前正在使用的内容以及我遇到的错误。注意:我删除了所有核心数据集成,并尽可能地简化了我的测试,以试图找出问题所在。
对象接口
@interface BLAuthor : NSObject
@property (nonatomic, retain) NSString *name;
@end
@interface BLVolume : NSObject
@property (nonatomic, retain) NSString *id;
@property (nonatomic, retain) NSSet *authors;
@end
映射和请求操作
RKObjectMapping *volumeMapping = [RKObjectMapping mappingForClass:[BLVolume class]];
[volumeMapping addAttributeMappingsFromDictionary:@{ @"id": @"id" }];
RKObjectMapping *authorMapping = [RKObjectMapping mappingForClass:[BLAuthor class]];
[authorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil
toKeyPath:@"name"]];
[volumeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"volumeInfo.authors"
toKeyPath:@"authors"
withMapping:authorMapping]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:volumeMapping
pathPattern:nil
keyPath:@"items"
statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://example.com/?q=123"]];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"Success! %@", result);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
NSOperationQueue *operationQueue = [NSOperationQueue new];
[operationQueue addOperation:operation];
JSON sn-p
{
"items": [
{
"id": "abc",
"volumeInfo": {
"authors": [
"123"
]
}
}
结果数据 - 请注意,
Success! <RKMappingResult: 0x8468560, results={
items = (
"<BLVolume = 0x08463F60 | id = mikPQFhIPogC | authors = {(
<BLAuthor = 0x08466250 | name = 123 ({
HTTP = {
request = {
URL = \"https://example.com/?q=123";
headers = {
};
method = GET;
};
response = {
URL = \"https://example.com/?q=123\";
headers = {
\"Cache-Control\" = \"private, max-age=0, must-revalidate, no-transform\";
\"Content-Type\" = \"application/json; charset=UTF-8\";
Date = \"Sun, 23 Jun 2013 00:41:01 GMT\";
Etag = \"\\\"I09ELXbrmOlE-RFCkDsRbIJj278/gPh8_OxpfA9YHXz_P_25F8A4orw\\\"\";
Expires = \"Sun, 23 Jun 2013 00:41:01 GMT\";
Server = GSE;
\"Transfer-Encoding\" = Identity;
\"X-Content-Type-Options\" = nosniff;
\"X-Frame-Options\" = SAMEORIGIN;
提前感谢任何可以帮助我解决此问题的人!我束手无策 - 试图从我的测试中删除尽可能多的变量,并搜索了网络和 RestKit 的源代码以寻找原因。
【问题讨论】:
标签: objective-c json restkit