如何修复RestKit JSON序列化问题

时间:2014-05-01 14:23:45

标签: ios objective-c json restkit restkit-0.20

我正在尝试使用RestKit 0.2将一个json对象数组(有时数组只包含一个对象)发送到REST服务

对于包含一个对象的数组,我想要发送到服务的内容应该类似于这样:

{[
      {
         "Latitude":0,
         "Location":"Nowhere, FL",
         "Id":25699,
         "Version":0,
         "Name":"TEST Customer Airport 2 created by tstUser",
         "TimeZone":":America\/New York",
         "Longitude":0,
         "Code":"XXXX987",
         "IsUserDefinedAirportCode":true
      }
]}

这里是RestKit根据跟踪输出在request.body中发送的内容:

{
   "\/AirportCode\/UpsertCustomerAirportCodes":[
      {
         "Latitude":0,
         "Location":"Nowhere, FL",
         "Id":25699,
         "Version":0,
         "Name":"TEST Customer Airport 2 created by tstUser",
         "TimeZone":":America\/New York",
         "Longitude":0,
         "Code":"XXXX987",
         "IsUserDefinedAirportCode":true
      }
   ]
}

json的第一个元素是我正在调用的服务的资源/操作路径。为什么RestKit会把它放在Json中?

详细...

要发送此信息,我有一个ApiAirportCode模型对象:

#import <Foundation/Foundation.h>

@interface ApiAirportCode : NSObject <NSCoding>

@property (nonatomic, copy) NSString *Code;
@property (nonatomic, copy) NSNumber *Id;
@property (nonatomic, copy) NSNumber *Latitude;
@property (nonatomic, copy) NSString *Location;
@property (nonatomic, copy) NSNumber *Longitude;
@property (nonatomic, copy) NSString *Name;
@property (nonatomic, copy) NSString *TimeZone;
@property (nonatomic, assign) bool IsUserDefinedAirportCode;

@end

以下是映射:

RKObjectMapping* airportCodePostMap = [RKObjectMapping mappingForClass:[ApiAirportCode class]];
[airportCodePostMap addAttributeMappingsFromArray:@[@"Code", @"Id", @"Latitude", @"Location", @"Longitude", @"Name", @"TimeZone", @"IsUserDefinedAirportCode"]];
RKRequestDescriptor* airportCodePostReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[airportCodePostMap inverseMapping] objectClass:[ApiAirportCode class] rootKeyPath:@"/AirportCode/UpsertCustomerAirportCodes" method:RKRequestMethodPOST];
[_ObjectManager addRequestDescriptor:airportCodePostReqDescriptor];

以下是对服务的调用:

- (void) saveCustomAirportCodesToServer:(NSArray*) customAirports
{
    _ObjectManager.requestSerializationMIMEType=RKMIMETypeJSON;
    [_ObjectManager postObject:customAirports path:@"/AirportCode/UpsertCustomerAirportCodes" parameters:NULL success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        // Do something


    } failure:^(RKObjectRequestOperation *operation, NSError *error) {

        ApiPayload* errorPld = [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey][0];

    }];

    // Do something...
}

以下是RestKit的跟踪输出:

  

2014-05-01 10:17:39.286 Flightdocs [2447:60b] D   restkit.object_mapping:RKMappingOperation.m:952开始映射   操作... 2014-05-01 10:17:39.287 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:953执行映射   操作:对于&_ 39; __ NSDictionaryM&#39;   宾语。映射来自对象的值   ((null))对象{}与对象映射(null)2014-05-01   10:17:39.287 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:550 Mapping属性   价值keyPath&#39;代码&#39;代码&#39;代码&#39; 2014-05-01 10:17:39.287   Flightdocs [2447:60b] T restkit.object_mapping:RKMappingOperation.m:572   来自keyPath&#39; Code&#39;的映射属性值代码&#39;价值:XXXX98765   2014-05-01 10:17:39.287 Flightdocs [2447:12ef7] D   restkit.object_mapping:RKPropertyInspector.m:130缓存属性   检查Class&#39; NSMutableDictionary&#39;:{       fileHFSFlags = {           isPrimitive = 1;           keyValueCodingClass = NSNumber;           name = fileHFSFlags;       };       fileHFSResourceForkSize = {           isPrimitive = 1;           keyValueCodingClass = NSNumber;           name = fileHFSResourceForkSize;       }; 2014-05-01 10:17:39.287 Flightdocs [2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping属性   价值keyPath&#39; Id&#39;到了&#39; Id&#39; 2014-05-01 10:17:39.288   Flightdocs [2447:60b] T restkit.object_mapping:RKMappingOperation.m:572   来自keyPath&#39; Id&#39;的映射属性值到了&#39; Id&#39;。价值:25699   2014-05-01 10:17:39.288 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:550 Mapping属性   价值keyPath&#39;纬度&#39;到纬度&#39; 2014-05-01 10:17:39.288   Flightdocs [2447:60b] T restkit.object_mapping:RKMappingOperation.m:572   来自keyPath&#39; Latitude&#39;的映射属性值至纬度&#39;值:0   2014-05-01 10:17:39.288 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:550 Mapping属性   价值keyPath&#39;位置&#39;到位置&#39; 2014-05-01 10:17:39.288   Flightdocs [2447:60b] T restkit.object_mapping:RKMappingOperation.m:572   来自keyPath&#39; Location&#39;的映射属性值到位置&#39;值:   无处,FL 2014-05-01 10:17:39.289 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:550 Mapping属性   价值keyPath&#39;经度&#39;到经度&#39; 2014-05-01 10:17:39.289   Flightdocs [2447:60b] T restkit.object_mapping:RKMappingOperation.m:572   来自keyPath&#39;经度&#39;的映射属性值到经度&#39;。值:   0 2014-05-01 10:17:39.289 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:550 Mapping属性   价值keyPath&#39;名称&#39;到&#39;姓名&#39; 2014-05-01 10:17:39.289   Flightdocs [2447:60b] T restkit.object_mapping:RKMappingOperation.m:572   来自keyPath&#39;名称&#39;的映射属性值到&#39;姓名&#39;。价值:测试   客户机场2由tstUser创建2014-05-01 10:17:39.289   Flightdocs [2447:60b] T restkit.object_mapping:RKMappingOperation.m:550   映射属性值keyPath&#39; TimeZone&#39;到了TimeZone&#39; 2014-05-01   10:17:39.289 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:572映射的属性值   来自keyPath&#39; TimeZone&#39;到了TimeZone&#39;。价值::美国/纽约   2014-05-01 10:17:39.289 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:550 Mapping属性   value keyPath&#39; IsUserDefinedAirportCode&#39;到'IsUserDefinedAirportCode&#39;   2014-05-01 10:17:39.290 Flightdocs [2447:60b] T   restkit.object_mapping:RKMappingOperation.m:572映射的属性值   来自keyPath&#39; IsUserDefinedAirportCode&#39;到'IsUserDefinedAirportCode&#39;。   价值:1 2014-05-01 10:17:39.290 Flightdocs [2447:60b] D   restkit.network:RKObjectParameterization.m:128已序列化   __NSCFBoolean keypath到__NSCFBoolean(1)2014-05-01 10:17:39.290 Flightdocs [2447:60b] D   restkit.object_mapping:RKMappingOperation.m:1021完成映射   操作成功... 2014-05-01 10:17:39.294 Flightdocs [2447:60b]   T restkit.network:RKObjectRequestOperation.m:178 POST   &#39; https://rsw-dev-05.flightdocs.com/AirportCode/UpsertCustomerAirportCodes&#39 ;:   request.headers = {       接受=&#34; application / json&#34 ;;       &#34;接受 - 语言#34; =&#34; en; q = 1,fr; q = 0.9,de; q = 0.8,zh-Hans; q = 0.7,zh-Hant; q = 0.6,ja; q = 0.5&#34 ;;       授权=&#34;承载1QgmbgUMk5Z-g5W8BtDZjOIok_NIAVuaL9b6kJJybdObQv3MZVShxWS098L1M1iP7DcGsV2nYfIK-FE6409hPN8WVQAJdNwh1VKRCY0STTKpViKown53Mq0B5cl-EbgIhBOF0LvTf0xwQnsLBAOl-21QYmZyNzfEbXGNhNuX3Cn06oHdZIzxLvpvwTATnVG-hCTQvAl04Oh3wzKWu3UAt7Nnbe_pZDIhLCpPFwyIw3aI2ife5fbWpoEcRBGbU0Bob8o2vrCciQiLWPCqsN2sepztD6A1XIPWObPhgn2sBGyrgSaJN_MyG6141UAKGBTDyYPWVMIVA6wx-AFtc6116goKtHc-FV7COJl_TuAWw7itc-543Rgmu_m1jKmRQT78bzit1JxG_j0YsJ4nB2hVHdUstV3QKAGiPUQl9PJxBRyHiUhwkRFwxIEAtSk9NsjfeehJJjaEq9Dr3jkfh6S8M6-AUa88q8DxNE-xman84dUsqeBBVigoVP-PGkgqv8fkU3DE2M4vVYdlyAHGvR8okA&#34 ;;       &#34;内容类型&#34; =&#34; application / json;字符集= UTF-8&#34 ;;       &#34;用户代理&#34; =&#34; Flightdocs / 1.4.7(iPad模拟器; iOS 7.1; Scale / 1.00)&#34 ;; }   request.body = {&#34; / AIRPORTCODE / UpsertCustomerAirportCodes&#34;:[{&#34;标识&#34;:25699&#34;纬度&#34;:0,&#34;位置&#34 ;: &#34;无处,   FL&#34;,&#34;名称&#34;:&#34; TEST客户机场2由。创建   tstUser&#34;&#34;时区和#34;:&#34;:美国/新   纽约&#34;&#34;经度&#34;:0,&#34;代码&#34;:&#34; XXXX98765&#34;&#34; IsUserDefinedAirportCode&#34;:真}]}

1 个答案:

答案 0 :(得分:1)

我明白了......

我的问题是使用rootKeyPath参数到RKRequestDescriptor requestDescriptorWithMapping。这应该是零。

我传递了资源/操作名称,因为我认为RestKit正在使用它知道何时使用此请求描述符(就像使用withPathPattern参数映射响应一样)。不确定为什么不需要传递一些东西来指示withPathPattern。

所以这行代码:

RKRequestDescriptor* airportCodePostReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[airportCodePostMap inverseMapping] objectClass:[ApiAirportCode class] rootKeyPath:@"/AirportCode/UpsertCustomerAirportCodes" method:RKRequestMethodPOST];

应该是

RKRequestDescriptor* airportCodePostReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[airportCodePostMap inverseMapping] objectClass:[ApiAirportCode class] rootKeyPath:nil method:RKRequestMethodPOST];