gpt4 book ai didi

ios - 使用 RESTKit 映射带有括号的 JSON 响应

转载 作者:行者123 更新时间:2023-11-29 03:43:10 25 4
gpt4 key购买 nike

我正在使用 RESTKit 执行 GET 请求,并且需要一些映射 JSON 响应的帮助。这是我需要映射的响应:

{"limit_hit":false,"providers":
[{"id":876553,
"name":"Cooper, Bradley N, DDS",
"specialty_groups":["Other Provider"],
"tags":[],
"has_comments":false,
"number_of_comments":0,
"locations":
[{"address":"1234 Rifle Range Road, El Cerrito, CA, 94530",
"providers_at_address_count":1,
"client_product_count":0,
"non_client_product_count":2,
"address_lines":["1234 Rifle Range Road, El Cerrito, CA, 94530"],
"address_id":234578,
"specialty_groups":
[{"specialty_group":"Other Provider"}],
"provider_types":
[{"provider_type":"Other Provider"}]},

{"address":"7501 Mission Rd, Shawnee Mission, KS, 66208",
"providers_at_address_count":2,
"client_product_count":0,
"non_client_product_count":2,
"address_lines":["7654 Main S, El Cerrito, CA, 94530"],
"address_id":654432,
"specialty_groups":
[{"specialty_group":"Other Provider"}],
"provider_types":
[{"provider_type":"Other Provider"}]
}]
}]
}

我希望能够映射这两个地址,但我不知道如何做。我目前能做的就是映射 id、name、has_comments 和 number_of_comments(我正在使用“providers”的键路径)。这是我当前的 map 提供商:

+ (RKMapping *)searchMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ProviderSearch class]];
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"doctorID",
@"name": @"name",
}];
return mapping;
}

我究竟做错了什么,该如何解决?

最佳答案

创建另一种方法来返回位置的映射,然后将该映射与原始映射关联起来。像这样:

// ProviderLocation.m
+ (RKObjectMapping *)objectMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ProviderLocation class]];
[mapping addAttributeMappingsFromDictionary:@{
@"address": @"address",
...
}];
return mapping;
}

关系:

+ (RKObjectMapping *)searchMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ProviderSearch class]];
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"doctorID",
@"name": @"name",
}];

RKObjectMapping *locationsMapping = [ProviderLocation objectMapping];
[mapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"locations" toKeyPath:@"locations" withMapping:locationsMapping]];

return mapping;
}

只需记住在 ProviderLocation.h 中创建一个名为 locations 的 NSArray 属性即可。

关于ios - 使用 RESTKit 映射带有括号的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086412/

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