gpt4 book ai didi

ios - 如何在休息套件中解析此响应?

转载 作者:行者123 更新时间:2023-11-29 12:04:27 26 4
gpt4 key购买 nike

我完全无法在 restkit 中解析这种响应。我正在使用这个 link但我无法理解如何解析此响应。我们将不胜感激。

“groups”: {
"group1": [
{
"email": "blake@restkit.org",
"favorite_animal": "Monkey"
},
{
"email": "slake@restkit.org",
"favorite_animal": "Donkey"
}
],
"group2": [
{
"email": "sarah@restkit.org",
"favorite_animal": "Cat"
},
{
"email": "varah@restkit.org",
"favorite_animal": "Cow"
}
]
}

我正在使用下方映射。

@interface GroupResponse : NSObject

@property (nonatomic, strong) NSArray *Groups;

+ (RKObjectMapping *)mapping;

@end

@implementation GroupResponse

+ (RKObjectMapping *)mapping {

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class]];
[objectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@“groups” toKeyPath:@“Groups” withMapping:[GroupData mapping]]];

return objectMapping;
}
@end



@interface GroupData : NSObject

@property (nonatomic, strong) NSString *groupName;
@property (nonatomic, strong) NSString *arrPersons;

+ (RKObjectMapping *)mapping;

@end

@implementation GroupData

+ (RKObjectMapping *)mapping {

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class]];

objectMapping.forceCollectionMapping = YES;
[objectMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"groupName"];

[objectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@“(groupName)” toKeyPath:@"arrPersons" withMapping:[Person mapping]]];

return objectMapping;
}

@end


@interface Person : NSObject

@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSString *favAnimal;

+ (RKObjectMapping *) mapping;
@end

@implementation Person

+ (RKObjectMapping *)mapping {

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class]];

[objectMapping addAttributeMappingsFromDictionary:@{@"email" : @"email",
@"favorite_animal" : @"favAnimal"}];

return objectMapping;
}

@end

每次 arrPersons 为零。在这种情况下如何进行适当的映射。

最佳答案

这个属性:

@property (nonatomic, strong) NSString *arrPersons;

实际上应该是可变数组,而不是字符串类型:

@property (nonatomic, strong) NSMutableArray *arrPersons;

因为嵌套的 JSON 数组无法转换为字符串,并且您的映射表明它应该被处理为 Person 对象的数组。

关于ios - 如何在休息套件中解析此响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35595715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com