gpt4 book ai didi

ios - RestKit复杂而简单的JSON RKObjectMapping几乎可以工作,但是

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

我一直在研究这个问题。以下是一些发现,将有助于在RestKit中映射此JSON响应

JSON响应对象包含三个顶级对象:
位置是一个数组
cityKey对象
stateKey对象
Since Restkit is written in Objective-C, I looked at it as if I were going to directing映射这些对象并解析出数据

我编写了以下代码来映射Location Class \ Object的NSDictionary部分:

    RKObjectMapping* locationMapping = [RKObjectMapping       mappingForClass:[Location class]];
[locationMapping addAttributeMappingsFromDictionary:@{
@"distance": @"distance",
@"major": @"major",
@"minor": @"minor" }];

我为整个class \ object位置编写了以下代码://这也应该是NSDictionary
    RKObjectMapping *locationsMapping = RKObjectMapping       mappingforclass: [Locations class]];

This appears to be a mapping array should be a dictionary.

[locationsMapping addAttributeMappingsFromArray:@[@"locations", @"cityKey",@"stateKey"]];

我看到在将Array用于整个对象Locations时出现错误。这将运行并返回以下内容的部分成功结果:
RKResponseDescriptor *locationDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:locationMapping method:RKRequestMethodAny pathPattern:@"/location" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
[[RKObjectManager sharedManager] addResponseDescriptor:locationDescriptor];



[[RKObjectManager sharedManager] postObject:nil path:(_enterLocation) parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"It Worked: %@", [mappingResult array]);


} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"It Failed: %@", error);

}];

This returns the successful responseJSONdata, but mappingResult dictionary returns:

2014-05-07 17:28:39.770 MyApp[163:60b] It Worked: {
locations = (
);

}

The actual JSONOBJECTWITHDATA returned as response.body is:

response.body={
  "locations": [
    {
      "distance": 0.5, 
      "major": 2, 
      "minor": 4, 
     
    }, 
    {
      "distance": 1.0, 
      "major": 2, 
      "minor": 11, 
     
    }
  ], 
  "cityKey": "12", 
  "stateKey": "41"
}

通过阅读,

Will RestKit's dynamic mapping solve this complex JSON mapping?



Feeding parsed data to RKMapperOperation throws NSUnknownKeyException

我知道我需要更改RestKit映射的方法。对于复杂的情况

即使在简单的情况下,我也只返回指向该对象的指针。
 RKObjectMapping *statusResponseMapping = [RKObjectMapping mappingForClass:[StatusResponse class]];

    [statusResponseMapping addAttributeMappingsFromDictionary:@{@"status":@"status"}]; 

RKResponseDescriptor *statusResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statusResponseMapping method:RKRequestMethodPOST pathPattern:nil keyPath:@"response" statusCodes:statusCodes];

我从服务器收到正确的响应...但是只得到一个指针
 response.body={
  "response": {
    "status": "ok"
  }
}
2014-05-07 17:11:47.362 MyApp[145:60b] It Worked: {
    response = "<StatusResponse: 0x16ee2e10>";

}

我缺少关键步骤或映射定义。我几乎达到了预期的结果。

在简单和复杂的情况下,我需要怎么做才能获得理想的结果?

使用以下新信息更新了我的问题

2014年5月8日更新,我将locationsMapping代码更改为...
RKObjectMapping *locationsMapping = [RKObjectMapping mappingForClass:[Locations class]];
[locationsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"locations" toKeyPath:@"locations" withMapping:locationMapping]];

这就使它更接近正确映射。结果是...
2014-05-08 13:28:17.795 MyApp[165:60b] It Worked: {
locations = (
"<Locations: 0x14e69a10>",
"<Locations: 0x14e695c0>"
);

这几乎是正确的。我似乎缺少cityKey和StateKey的映射。我尚未定义Description方法。这是返回的原始JSON数据...
response.body={
"locations": [
{
"distance": 0.6,
"major": 2,
"minor": 4

},
{
"distance": 1.0,
"major": 2,
"minor": 11

}
],
"cityKey": "11",
"stateKey": "21"
}

映射不返回cityKey和stateKey ...映射定义中仍然缺少某些内容吗?

更改位置后于2014年5月9日更新
RKObjectMapping *locationsMapping = [RKObjectMapping mappingForClass:[Locations class]];

[locationsMapping addAttributeMappingsFromArray:@[
@"cityKey",@"stateKey"]];

[locationsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"locations" toKeyPath:@"locations" withMapping:locationMapping]];


response.body={
  "locations": [
    {
      "distance": 0.6, 
      "major": 2, 
      "minor": 4
    }, 
    {
      "distance": 1.0, 
      "major": 2, 
      "minor": 11
    }
  ], 
  "cityKey": "10", 
  "stateKey": "15"
}
2014-05-09 13:18:17.669 MyApp[211:60b] It Worked: {
    locations =     (
        "<Locations: 0x1780341e0>",
        "<Locations: 0x170027c40>"
    );
}

仍然缺少cityKey和stateKey ...不确定下一步该怎么做?

更新于2014年5月12日以回应响应者...

我有两个响应描述符。我错过了在原始说明中张贴一个。
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:locationMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"locations"  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    RKResponseDescriptor *locationDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:locationsMapping method:RKRequestMethodAny pathPattern:@"/location" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    
    [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
    [[RKObjectManager sharedManager] addResponseDescriptor:locationDescriptor];

我按建议更改了结构,但仍然缺少cityKey和stateKey ...我应该只使用一个响应描述符吗?

我找到了“使用多个ResponseDescriptor?”的答案。在本文中 http://www.softwarepassion.com/parsing-complex-json-with-objective-c-and-restkit/

当我回顾作者如何定义他的POJO时,我发现只需要一个ResponseDescriptor。其余的在关系的映射和定义中处理...

2014年5月15日更新

我测试了响应描述符。实际上只有一个人在努力产生“成功”结果。 locationDescriptor根本不起作用。单独使用时会产生错误。用作配对的一部分时被忽略。始终使用位置数据传递“工作正常”的描述符是带有keyPath的@@“locations”的responseDescriptor。

我读了 http://blog.mobilejazz.cat/ios-using-kvc-to-parse-json/

我有一个关于“...是您创建的自定义类的预期日志输出,但没有描述方法实现的问题”。描述方法的实现是什么意思?我需要添加什么才能将以下内容更改为映射结果的NSArray或NSDictionary?您能否根据以下位置分享一个示例?
2014-05-09 13:18:17.669 MyApp[211:60b] It Worked: {
    locations =     (
        "<Locations: 0x1780341e0>",
        "<Locations: 0x170027c40>"
    );

2014年5月16日更新建议对类实现文件添加NSSTRING说明
response.body={
"response": {
"status": "ok"
}
}
2014-05-16 10:32:26.260 MyApp[202:60b] It Worked: {
response = "status: ok";
}

因此,我计算了NSString描述方法的实现情况并可以正常工作。

更新的第二部分是使用方法描述器,因为我可以看到我没有将映射的JSON替换为数据结构值,而我却获取了NULL
response.body={
"locations": [
{
"distance": 0.0,
"major": 2,
"minor": 8,

},
{
"distance": 3.5,
"major": 2,
"minor": 9,

},
{
"distance": 2.6575364531836625,
"major": 2,
"minor": 10,

}
],
"cityKey": "10",

"stateKey": "21"
}
2014-05-16 09:48:08.328 MyApp[189:60b] It Worked: {
locations = (
"location: (null) city: (null) state: (null)",
"location: (null) city: (null) state: (null)",
"location: (null) city: (null) state: (null)"
);
}

第一个问题是我应该为字典数组中的三个位置以及一个cityKey和一个stateKey获得一个位置{distance,major,minor}。这是映射代码
RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[Location class]];
[locationMapping addAttributeMappingsFromDictionary:@{
@"distance": @"distance",
@"major": @"major",
@"minor": @"minor"
}];


RKObjectMapping *locationsMapping = [RKObjectMapping mappingForClass:[Locations class]];

[locationsMapping addAttributeMappingsFromArray:@[ @"cityKey",@"stateKey"]];



[locationsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"locations" toKeyPath:@"locations" withMapping:locationMapping]];

//05/16/2014 this ResponseDesciptor is working
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:locationsMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"locations" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

为什么要为每个位置返回多个cityKey和stateKey?为什么当JSON数据返回时结果显示为null?

当keyPath设置为nil而不是@“locations”时,2014年5月19日更新异常
2014-05-19 10:53:53.902 MyApp[245:4907] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Locations 0x17022d960> valueForUndefinedKey:]: this class is not key value coding-compliant for the key locations.'
*** First throw call stack:

代码提升IT RestKit代码行364
if ([self.dataSource respondsToSelector:@selector(mappingOperationShouldSetUnchangedValues:)] && [self.dataSource mappingOperationShouldSetUnchangedValues:self]) return YES;

id currentValue = [self.destinationObject valueForKeyPath:keyPath];
if (currentValue == [NSNull null]) {
currentValue = nil;
}

位置类头文件
#import <Foundation/Foundation.h>

@class Location;


@interface Locations : NSObject




@property (nonatomic, copy) Location *location;
@property (nonatomic, copy) NSString *cityKey;
@property (nonatomic, copy) NSString *stateKey;


@end

位置类实现文件
#import "Locations.h"

@implementation Locations

-(NSString *)description {

return [NSString stringWithFormat:@"location: %@ cityKey: %@ stateKey: %@", self.location, self.cityKey, self.stateKey];
}

@end

位置更改后的位置类属性
@property (nonatomic, copy) Location *location; 


@property (nonatomic, copy) NSMutableArray *locations;

keyPath为nil时收到期望的结果
response.body={
"locations": [
{
"distance": 0.0,
"major": 2,
"minor": 8

},
{
"distance": 3.5,
"major": 2,
"minor": 9

},
{
"distance": 2.6575364531836625,
"major": 2,
"minor": 10

}
],
"cityKey": "1",

"stateKey": "1"
}
2014-05-19 14:19:45.040 MyApp[290:60b] It Worked: {
"<null>" = "location: (\n \"distance: 0 major: 2 minor: 8 \",\n \"distance: 3.5 major: 2 minor: 9 \",\n \"distance: 2.657536453183662 major: 2 minor: 10 \"\n) cityKey: 1 stateKey: 1";
}

哪个解决了这个问题。

最佳答案

创建locationsMapping时,应使用@"locations"locationMapping作为关系添加(而不是直接作为属性)。

您应该有一个使用locationsMapping的响应描述符。

这个:

2014-05-07 17:11:47.362 MyApp[145:60b] It Worked: {
response = "<StatusResponse: 0x16ee2e10>";
}

是您创建的自定义类的预期日志输出,但该类没有 description方法实现。
RKObjectMapping *locationsMapping = [RKObjectMapping mappingForClass:[Locations class]];
[locationsMapping addAttributeMappingsFromArray:@[@"cityKey", @"stateKey"]];
[locationsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"locations" toKeyPath:@"locations" withMapping:locationMapping]];

Locations类上添加:
- (NSString *)description
{
return [NSString stringWithFormat:@"city:%@ state:%@ locations:%@", self.cityKey, self.stateKey, self.locations];
}

Location类的类似方法。

关于ios - RestKit复杂而简单的JSON RKObjectMapping几乎可以工作,但是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23529494/

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