RestKit 0.20:映射复杂的动态嵌套 JSON

Posted

技术标签:

【中文标题】RestKit 0.20:映射复杂的动态嵌套 JSON【英文标题】:RestKit 0.20: mapping complex dynamic nested JSON 【发布时间】:2014-04-17 17:22:47 【问题描述】:

我对 ios 开发和 RestKit 框架(使用 0.20.3 版)非常熟悉。单实体映射就像一个魅力。然而,当谈到动态嵌套 JSON 的关系映射时,过去几天我一直在苦苦挣扎。我有以下我正在尝试映射的 JSON 响应:

 "chargerstations": [
    
      "csmd": 
        "id": 1190,
        "name": "Tammasaarenkatu  7",
        "Street": "Tammasaarenkatu",
        "House_number": "7",
        "Zipcode": "00180",
        "City": "Ruoholahti",
        "Municipality_ID": "091",
        "Municipality": "Helsinki",
        "County_ID": "01",
        "County": "Uusimaa",
        "Description_of_location": "",
        "Owned_by": "Helen",
        "Number_charging_points": 1,
        "Position": "(60.16172,24.90679)",
        "Image": "Kommer",
        "Available_charging_points": 1,
        "User_comment": "",
        "Contact_info": "",
        "Created": "2012-03-30 11:40:48",
        "Updated": "2013-04-16 11:52:47",
        "Station_status": 1,
        "Land_code": "FIN",
        "International_id": "FIN_01190"
      ,
      "attr": 
        "st": 
          "2": 
            "attrtypeid": "2",
            "attrname": "Availability",
            "attrvalid": "1",
            "trans": "Public",
            "attrval": ""
          ,
          "3": 
            "attrtypeid": "3",
            "attrname": "Location",
            "attrvalid": "1",
            "trans": "Street",
            "attrval": ""
          ,
          "6": 
            "attrtypeid": "6",
            "attrname": "Time limit",
            "attrvalid": "2",
            "trans": "No",
            "attrval": ""
          ,
          "7": 
            "attrtypeid": "7",
            "attrname": "Parking fee",
            "attrvalid": "2",
            "trans": "No",
            "attrval": false
          ,
          "21": 
            "attrtypeid": "21",
            "attrname": "Real-time information",
            "attrvalid": "2",
            "trans": "No",
            "attrval": ""
          ,
          "22": 
            "attrtypeid": "22",
            "attrname": "Public funding",
            "attrvalid": "4",
            "trans": "None",
            "attrval": ""
          ,
          "24": 
            "attrtypeid": "24",
            "attrname": "Open 24h",
            "attrvalid": "1",
            "trans": "Yes",
            "attrval": "1"
          
        ,
        "conn": 
          "1": 
            "1": 
              "attrtypeid": "1",
              "attrname": "Accessibility",
              "attrvalid": "6",
              "trans": "Cellular phone",
              "attrval": ""
            ,
            "4": 
              "attrtypeid": "4",
              "attrname": "Connector",
              "attrvalid": "32",
              "trans": "Mennekes-type 2 (IEC 62196-2) \n",
              "attrval": ""
            ,
            "5": 
              "attrtypeid": "5",
              "attrname": "Charging capacity",
              "attrvalid": "11",
              "trans": "400V 3-phase max 32A",
              "attrval": ""
            ,
            "17": 
              "attrtypeid": "17",
              "attrname": "Vehicle type",
              "attrvalid": "1",
              "trans": "All vehicles",
              "attrval": ""
            ,
            "18": 
              "attrtypeid": "18",
              "attrname": "Reservable",
              "attrvalid": "2",
              "trans": "No",
              "attrval": ""
            ,
            "20": 
              "attrtypeid": "20",
              "attrname": "Charge mode",
              "attrvalid": "3",
              "trans": "Mode 3",
              "attrval": ""
            ,
            "25": 
              "attrtypeid": "25",
              "attrname": "Fixed cable",
              "attrvalid": "2",
              "trans": "No",
              "attrval": ""
            
          
          "2": 
            "1": same structure as above
            
          
          "<conn no. x>": 
            "1": same structure as above
            
          
        
      
    
  ]

我创建了一个包含 2 个实体的核心数据模型; “ChargingStation”包含来自“csmd”和“attr.st”字典的数据,“Connector”包含来自 JSON 的动态嵌套部分 (attr.conn) 的数据。一个充电站可能有多个连接器,但一个连接器可能只属于一个充电站。因此,我定义了一个从“ChargingStation”到“Connector”的多对多关系,与“Connector”实体的反向一对一关系。

http://imgur.com/Q0mhlIi

我的模型是使用 mogenerator 生成的。这是我目前关于映射本身的代码(在 AppDelegate.m 中):

RKEntityMapping *chargingStationMapping = [RKEntityMapping mappingForEntityForName:@"ChargingStation"
                                                              inManagedObjectStore:managedObjectStore];
[chargingStationMapping addAttributeMappingsFromDictionary:@
                                                             @"attr.st.2.trans" : @"availabilityType",
                                                             @"csmd.City" : @"city",
                                                             @"csmd.Position" : @"coordinates",
                                                             @"csmd.House_number" : @"houseNumber",
                                                             @"csmd.id" : @"stationID",
                                                             @"csmd.International_id" : @"internationalID",
                                                             @"csmd.Description_of_location" : @"locationDescription",
                                                             @"csmd.Image" : @"locationImage",
                                                             @"attr.st.3.trans" : @"locationName",
                                                             @"csmd.name" : @"name",
                                                             @"csmd.Available_charging_points" : @"numChargingPoints",
                                                             @"attr.st.24.trans" : @"openingHours",
                                                             @"attr.st.7.trans" : @"parkingFee",
                                                             @"attr.st.22.trans" : @"publicFunding",
                                                             @"attr.st.21.trans" : @"realtimeInfo",
                                                             @"csmd.Street" : @"street",
                                                             @"attr.st.6.trans" : @"timeLimit",
                                                             @"csmd.Zipcode" : @"zipCode"
                                                             ];

chargingStationMapping.identificationAttributes = @[@"stationID"];


RKDynamicMapping *dynamicConnectorMapping = [[RKDynamicMapping alloc] init];
[dynamicConnectorMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) 
    RKEntityMapping *connectorMapping = [RKEntityMapping mappingForEntityForName:@"Connector"
                                                            inManagedObjectStore:managedObjectStore];
    NSDictionary *connectors = [representation valueForKeyPath:@"attr.conn"];
    for (NSString *attr in connectors) 
        NSDictionary *connector = [connectors objectForKey:attr];
        [connector enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) 
            NSLog(@"Attr from outer connector object: %@", attr);

            if ([key isEqualToString:@"1"]) 
                [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                             [NSString stringWithFormat:@"attr.conn.%@.%@.trans", attr, key]: @"accessibility"
                                                                             ];
             else if ([key isEqualToString:@"4"]) 
                [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                             [NSString stringWithFormat:@"attr.conn.%@.%@.trans", attr, key]: @"connector"
                                                                             ];
             else if ([key isEqualToString:@"5"]) 
                [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                             [NSString stringWithFormat:@"attr.conn.%@.%@.trans", attr, key]: @"chargingCapacity"
                                                                             ];
             else if ([key isEqualToString:@"17"]) 
                [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                             [NSString stringWithFormat:@"attr.conn.%@.%@.trans", attr, key]: @"vehicleType"
                                                                             ];
             else if ([key isEqualToString:@"18"]) 
                [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                             [NSString stringWithFormat:@"attr.conn.%@.%@.trans", attr, key]: @"reservable"
                                                                             ];
             else if ([key isEqualToString:@"20"]) 
                [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                             [NSString stringWithFormat:@"attr.conn.%@.%@.trans", attr, key]: @"chargeMode"
                                                                             ];
             else if ([key isEqualToString:@"23"]) 
                [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                             [NSString stringWithFormat:@"attr.conn.%@.%@.trans", attr, key]: @"manufacturer"
                                                                             ];
             else if ([key isEqualToString:@"25"]) 
                [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                             [NSString stringWithFormat:@"attr.conn.%@.%@.trans", attr, key]: @"fixedCable"
                                                                             ];
            
        ];
    

    connectorMapping.identificationAttributes = @[@"id"];
    [connectorMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"ChargingStation"
                                                                                     toKeyPath:@"chargingstation"
                                                                                   withMapping:chargingStationMapping]];

    return connectorMapping;
];

RKResponseDescriptor *connectorDescription = [RKResponseDescriptor responseDescriptorWithMapping:dynamicConnectorMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"chargerstations" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKResponseDescriptor *chargingStationDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:chargingStationMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"chargerstations.attr.conn" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:chargingStationDescriptor];
[objectManager addResponseDescriptor:connectorDescription];

[objectManager getObjectsAtPath:<URL_request>"
                     parameters:nil
                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
                            ChargingStation *cstation = [mappingResult firstObject];
                            NSLog(@"House number: %@", cstation.houseNumber);

 failure:^(RKObjectRequestOperation *operation, NSError *error) ];

当我尝试运行它时,我收到以下错误消息:

Assertion failure in -[RKEntityMapping addPropertyMapping:], /<my_project_directory>/Pods/RestKit/Code/ObjectMapping/RKObjectMapping.m:237
2014-04-21 18:30:55.200 ChargeIt[844:530b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath connector, one already exists...'

我认为这是因为我有两个 ResponseDescriptor 用于同一个父 keyPath,即“充电站”?我已经阅读了https://github.com/RestKit/RestKit/wiki/Object-mapping 的映射指南并查看了其他一些教程,以及关于 SO 的其他问题,但我仍然无法终生弄清楚我做错了什么。

如果我能得到任何帮助和/或推动正确的方向,我将不胜感激。

更新:我现在已经按照@Wain 建议的方法添加了修改后的代码,并且还升级到了Restkit 的0.23.1 版本。映射现在可以工作了(好吧,至少它完成了映射而没有任何错误:)...)。但是,当我尝试访问与充电站关联的连接器时,它返回一个空的 NSSet,并且在尝试记录 NSSet 时,我在日志中收到以下错误:

connectors = "<relationship fault: 0x9fb5fe0 'connectors'>";

我还查看了 SQLite 数据库,我注意到连接器实体在其表中获得了“ChargingStation”字段,但 ChargingStation 实体在其表中没有任何“连接器”字段。

RKEntityMapping *chargingStationMapping = [RKEntityMapping mappingForEntityForName:@"ChargingStation"
                                                                  inManagedObjectStore:managedObjectStore];
    [chargingStationMapping addAttributeMappingsFromDictionary:@
                                                                 @"attr.st.2.trans" : @"availabilityType",
                                                                 @"chargerstations.csmd.City" : @"city",
                                                                 @"csmd.Position" : @"coordinates",
                                                                 @"csmd.House_number" : @"houseNumber",
                                                                 @"csmd.id" : @"stationID",
                                                                 @"csmd.International_id" : @"internationalID",
                                                                 @"csmd.Description_of_location" : @"locationDescription",
                                                                 @"csmd.Image" : @"locationImage",
                                                                 @"attr.st.3.trans" : @"locationName",
                                                                 @"csmd.name" : @"name",
                                                                 @"csmd.Available_charging_points" : @"numChargingPoints",
                                                                 @"attr.st.24.trans" : @"openingHours",
                                                                 @"attr.st.7.trans" : @"parkingFee",
                                                                 @"attr.st.22.trans" : @"publicFunding",
                                                                 @"attr.st.21.trans" : @"realtimeInfo",
                                                                 @"csmd.Street" : @"street",
                                                                 @"attr.st.6.trans" : @"timeLimit",
                                                                 @"csmd.Zipcode" : @"zipCode"
                                                                 ];

    chargingStationMapping.identificationAttributes = @[@"stationID"];


    RKEntityMapping *connectorMapping = [RKEntityMapping mappingForEntityForName:@"Connector"
                                                                  inManagedObjectStore:managedObjectStore];
    connectorMapping.forceCollectionMapping = YES;
    [connectorMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"connectorID"];
    [connectorMapping addAttributeMappingsFromDictionary:@
                                                           @"(connectorID).1.trans": @"accessibility",
                                                           @"(connectorID).4.trans": @"connector",
                                                           @"(connectorID).5.trans": @"chargingCapacity",
                                                           @"(connectorID).17.trans": @"vehicleType",
                                                           @"(connectorID).18.trans": @"reservable",
                                                           @"(connectorID).20.trans": @"chargeMode",
                                                           @"(connectorID).23.trans": @"manufacturer",
                                                           @"(connectorID).25.trans": @"fixedCable"
                                                           ];

    [chargingStationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"attr.conn"
                                                                                           toKeyPath:@"connectors"
                                                                                         withMapping:connectorMapping]];

    RKResponseDescriptor *chargingStationDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:chargingStationMapping
                                                                                                    method:RKRequestMethodAny
                                                                                               pathPattern:nil
                                                                                                   keyPath:@"chargerstations"
                                                                                               statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [objectManager addResponseDescriptor:chargingStationDescriptor];
    [objectManager getObjectsAtPath:@"datadump.php?apikey=<API_KEY>&file=false&format=json&countrycode=FIN"
                         parameters:nil
                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
                                ChargingStation *cstation = [mappingResult firstObject];
                                NSLog(@"Connectors: %@", cstation);
                                NSLog(@"Connector info: %i", cstation.connectorsSet.count);
     failure:^(RKObjectRequestOperation *operation, NSError *error) ];

从我在日志中看到的内容来看,它正确地映射了所有内容(请参阅日志输出中相当长的附加部分)。有什么想法吗?

to object <ChargingStation: 0x9c84cf0> (entity: ChargingStation; id: 0x151c4f00 <x-coredata://77C8299D-FDDB-4810-8D22-C13538858DA1/ChargingStation/p18> ; data: 
    availabilityType = Public;
    city = nil;
    connectors = "<relationship fault: 0x9c84270 'connectors'>";
    coordinates = "(60.16172,24.90679)";
    houseNumber = 7;
    internationalID = "FIN_01190";
    locationDescription = "";
    locationImage = Kommer;
    locationName = Street;
    name = "Tammasaarenkatu  7";
    numChargingPoints = 1;
    openingHours = Yes;
    parkingFee = No;
    paymentMethods = nil;
    publicFunding = None;
    realtimeInfo = No;
    stationID = 1190;
    street = Tammasaarenkatu;
    timeLimit = No;
    zipCode = 00180;
) with object mapping (null)
2014-04-30 19:35:06.949 ChargeIt[6131:5613] D restkit.object_mapping:RKMappingOperation.m:766 Collection mapping forced for NSDictionary, mapping each key/value independently...
2014-04-30 19:35:06.950 ChargeIt[6131:5613] D restkit.object_mapping:RKMappingOperation.m:640 Mapping one to many relationship value at keyPath 'attr.conn' to 'connectors'
2014-04-30 19:35:06.951 ChargeIt[6131:3d13] D restkit.object_mapping:RKPropertyInspector.m:131 Cached property inspection for Class 'Connector': 
    accessibility =     
        isPrimitive = 0;
        keyValueCodingClass = NSString;
        name = accessibility;
    ;
    ...
    ;
    vehicleType =     
        isPrimitive = 0;
        keyValueCodingClass = NSString;
        name = vehicleType;
    ;

2014-04-30 19:35:06.984 ChargeIt[6131:5613] I restkit.core_data:RKInMemoryManagedObjectCache.m:94 Caching instances of Entity 'Connector' by attributes 'connectorID'
2014-04-30 19:35:07.042 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:541 Performing nested object mapping using mapping <RKRelationshipMapping: 0x9e4ce70 attr.conn => connectors> for data: 
    1 =     
        1 =         
            attrname = Accessibility;
            attrtypeid = 1;
            attrval = "";
            attrvalid = 6;
            trans = "Cellular phone";
        ;
        17 =         
            attrname = "Vehicle type";
            attrtypeid = 17;
            attrval = "";
            attrvalid = 1;
            trans = "All vehicles";
        ;
        18 =         
            attrname = Reservable;
            attrtypeid = 18;
            attrval = "";
            attrvalid = 2;
            trans = No;
        ;
        20 =         
            attrname = "Charge mode";
            attrtypeid = 20;
            attrval = "";
            attrvalid = 3;
            trans = "Mode 3";
        ;
        25 =         
            attrname = "Fixed cable";
            attrtypeid = 25;
            attrval = "";
            attrvalid = 2;
            trans = No;
        ;
        4 =         
            attrname = Connector;
            attrtypeid = 4;
            attrval = "";
            attrvalid = 32;
            trans = "Mennekes-type 2 (IEC 62196-2) \n";
        ;
        5 =         
            attrname = "Charging capacity";
            attrtypeid = 5;
            attrval = "";
            attrvalid = 11;
            trans = "400V 3-phase max 32A";
        ;
    ;

2014-04-30 19:35:07.043 ChargeIt[6131:5613] D restkit.object_mapping:RKMappingOperation.m:859 Starting mapping operation...
2014-04-30 19:35:07.044 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:860 Performing mapping operation: <RKMappingOperation 0x9cdcdd0> for 'Connector' object. Mapping values from object 
    1 =     
        1 =         
            attrname = Accessibility;
            attrtypeid = 1;
            attrval = "";
            attrvalid = 6;
            trans = "Cellular phone";
        ;
        17 =         
            attrname = "Vehicle type";
            attrtypeid = 17;
            attrval = "";
            attrvalid = 1;
            trans = "All vehicles";
        ;
        18 =         
            attrname = Reservable;
            attrtypeid = 18;
            attrval = "";
            attrvalid = 2;
            trans = No;
        ;
        20 =         
            attrname = "Charge mode";
            attrtypeid = 20;
            attrval = "";
            attrvalid = 3;
            trans = "Mode 3";
        ;
        25 =         
            attrname = "Fixed cable";
            attrtypeid = 25;
            attrval = "";
            attrvalid = 2;
            trans = No;
        ;
        4 =         
            attrname = Connector;
            attrtypeid = 4;
            attrval = "";
            attrvalid = 32;
            trans = "Mennekes-type 2 (IEC 62196-2) \n";
        ;
        5 =         
            attrname = "Charging capacity";
            attrtypeid = 5;
            attrval = "";
            attrvalid = 11;
            trans = "400V 3-phase max 32A";
        ;
    ;
 to object <Connector: 0x1965b460> (entity: Connector; id: 0x9e0a940 <x-coredata://77C8299D-FDDB-4810-8D22-C13538858DA1/Connector/p35> ; data: 
    accessibility = Other;
    chargeMode = "Mode 3";
    chargingCapacity = "400V 3-phase max 32A";
    chargingstation = "0x9c3f620 <x-coredata://77C8299D-FDDB-4810-8D22-C13538858DA1/ChargingStation/p35>";
    connector = "Type 2 + Schuko CEE 7/4";
    connectorID = 1;
    fixedCable = No;
    manufacturer = Manufacturer;
    reservable = No;
    vehicleType = "All vehicles";
) with object mapping (null)
2014-04-30 19:35:07.047 ChargeIt[6131:5613] D restkit.object_mapping:RKMappingOperation.m:821 Found nested mapping definition to attribute 'connectorID'
2014-04-30 19:35:07.048 ChargeIt[6131:5613] D restkit.object_mapping:RKMappingOperation.m:824 Found nesting value of '1' for attribute 'connectorID'
2014-04-30 19:35:07.048 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath '<RK_NESTING_ATTRIBUTE>'. Transforming from class '__NSCFString' to 'NSNumber'
2014-04-30 19:35:07.048 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath '<RK_NESTING_ATTRIBUTE>' to 'connectorID'
2014-04-30 19:35:07.049 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:484 Skipped mapping of attribute value from keyPath '<RK_NESTING_ATTRIBUTE> to keyPath 'connectorID' -- value is unchanged (1)
2014-04-30 19:35:07.049 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:507 Skipping attribute mapping for special keyPath '<RK_NESTING_ATTRIBUTE>'
2014-04-30 19:35:07.050 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath '1.4.trans'. Transforming from class '__NSCFString' to 'NSString'
2014-04-30 19:35:07.064 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath '1.4.trans' to 'connector'
   ....
    2014-04-30 19:35:07.118 ChargeIt[6131:5613] T restkit.object_mapping:RKMappingOperation.m:622 Mapped `NSSet` relationship object from keyPath 'attr.conn' to 'connectors'. Value: (
        <Connector: 0x1965b460> (entity: Connector; id: 0x9e0a940 <x-coredata://77C8299D-FDDB-4810-8D22-C13538858DA1/Connector/p35> ; data: 
        accessibility = "Cellular phone";
        chargeMode = "Mode 3";
        chargingCapacity = "400V 3-phase max 32A";
        chargingstation = "0x9c3f620 <x-coredata://77C8299D-FDDB-4810-8D22-C13538858DA1/ChargingStation/p35>";
        connector = "Mennekes-type 2 (IEC 62196-2) \n";
        connectorID = 1;
        fixedCable = No;
        manufacturer = Manufacturer;
        reservable = No;
        vehicleType = "All vehicles";
    )
    )

【问题讨论】:

您应该首先升级到版本 0.23.1(或至少 0.22.0)。 【参考方案1】:

如果可以的话,你真的应该改变 JSON,它不是很好。

问题在于动态映射中的循环,您在该循环中分析传入数据,然后尝试将大量映射添加到同一键路径。您需要采用不同的方法,以便拥有一个关系,该关系使用该关系处理所有连接器(链接到另一个映射)。

看看使用addAttributeMappingFromKeyOfRepresentationToAttribute:。第一个映射具有到第二个映射的relationship 并指定源。密钥路径attr.conn。第二个映射使用表示键来处理未知键。

【讨论】:

已升级到版本 0.23.1,但在尝试设置以下内容时出现错误:objectManager.managedObjectStore = managedObjectStore ...显然,在 RKObjectManager 对象类型上找不到属性 managedObjectStore。无论如何,关于 JSON - 我可以:)。至于你的建议,我真的没有这么想过。非常感谢您的意见!我一定会尝试你的方法,并在我得到它后发布一个工作示例。 在上面添加了更新的代码。非常感谢到目前为止的帮助 - 它让我顺利完成了映射。不过,我遇到了另一个问题,我还没有弄清楚(请参阅更新的问题)。关于 managedStore 的错误;我只是忘记将#import 添加到我的 .pch 文件中... connectors = "&lt;relationship fault: 0x9fb5fe0 'connectors'&gt;"; 只是表示关系数据没有加载到内存中,这不是错误,并且关系可以填充或为空(但如果不加载它就无法判断)。您是否尝试过访问关系中的项目,而不仅仅是记录关系? 我尝试了以下方法(使用 fetchRequest):for(ChargingStation *cstation in fetchedObjects) NSLog(@"Connectors for %@: %@", cstation.name,[cstation.connectors allObjects]); for(Connector *connector in [cstation connectors]) NSLog(@"Manufacturer %@", connector.manufacturer); 尝试访问“制造商”属性会产生错误:“无法在对象连接器的前向类中找到”。此外,我的映射没有完全按预期工作。有许多 ChargingStation 对象没有任何连接器。还在调试中 通过设置[RKEntityMapping setEntityIdentificationInferenceEnabled:NO]解决并移除了连接器映射的标识属性。这种方式总是为连接器对象创建新实例。 @Wain:非常感谢您的所有帮助和输入,为我指明了正确的方向!没有你就无法解决这个问题。

以上是关于RestKit 0.20:映射复杂的动态嵌套 JSON的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 0.20 嵌套对象映射(对象树的路径不同)

RestKit 0.20 嵌套对象映射问题使用 API

RestKit 0.20:在 JSON 中使用动态扁平化层次结构

RestKit 0.20:restkit 对象映射使属性映射加倍

RestKit 0.20 映射问题

使用 RestKit 0.20 的对象映射关系