没有响应描述符匹配加载的响应

当我尝试映射包含字符串和自定义对象数组的复杂对象时,出现问题。 我收到错误:没有响应描述符匹配加载的响应。

我试图将这个JSON映射到一个复杂的对象:

    {
  "productID" : 90,
  "productName" : "brooklyn decker",
  "brandID" : 58,
  "productPictureURLs" : null,
  "colors" : [ ],
  "sizes" : [ ],
  "configurationsAvailable" : [ {
    "size" : "m",
    "color" : "red",
    "price" : 100.00,
    "onSalePrice" : null,
    "onSale" : false,
    "favorited" : false,
    "quantityInShoppingCart" : 0,
    "productPictureURLs" : null,
    "productVariantId" : 1171
  }, {
    "size" : "b",
    "color" : "blue",
    "price" : 100.00,
    "onSalePrice" : null,
    "onSale" : false,
    "favorited" : false,
    "quantityInShoppingCart" : 0,
    "productPictureURLs" : null,
    "productVariantId" : 1173
  }, {
    "size" : "b",
    "color" : "grey",
    "price" : 100.00,
    "onSalePrice" : null,
    "onSale" : false,
    "favorited" : false,
    "quantityInShoppingCart" : 0,
    "productPictureURLs" : null,
    "productVariantId" : 1174
  } ],
  "descriptionBody" : "description"
}

我使用这些类:`@interface TransferSizeAndColorConfiguration:NSObject

    @property (nonatomic) int64_t * productVariantId;

    @property (nonatomic, strong) NSString * color;
    @property (nonatomic, strong) NSString * size;

    @property (nonatomic, strong) NSDecimalNumber * price;
    @property (nonatomic, strong) NSDecimalNumber * onSalePrice;
    @property (nonatomic) BOOL * onSale;
    @property (nonatomic) BOOL * favorited;
    @property (nonatomic) int * quantityInShoppingCart;
    @property (nonatomic, strong) NSArray <TransferObjectString *> *productPictureURLs;

`
and for the product:

    @property (nonatomic) int64_t productID;
@property (nonatomic) int64_t brandID;
@property (nonatomic) NSArray <TransferObjectString *> * colors;
@property (nonatomic) NSArray <TransferObjectString *> * sizes;
@property (nonatomic) NSArray <TransferObjectString *> * productPictureURLs;
@property (nonatomic) NSArray <TransferSizeAndColorConfiguration *> * configurationsAvailable; // Class not yet implemented
@property (nonatomic) NSString * productName;
@property (nonatomic) NSString * descriptionBody;;

我使用这里映射它们:

        // You need a mapping for the cofiguration of size and color as well and you have to set up a relation between the configuration and the product
    RKObjectMapping *configurationForSizeAndColorMapping = [RKObjectMapping mappingForClass:[TransferSizeAndColorConfiguration  class]];
    [configurationForSizeAndColorMapping addAttributeMappingsFromArray:@[@"productVariantId",  @"color",  @"size",  @"price", @"onSalePrice", @"onSale", @"favorited", @"quantityInShoppingCart"]];


    // The prduct picture urls are stored in an array and therefore need to have a mapping to other objects that hold only the urls
    // Map multiple objects to one keypath
    RKObjectMapping *productPictureStringMapping = [RKObjectMapping mappingForClass:[TransferObjectString class]];
    [productPictureStringMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"string"]];

    [configurationForSizeAndColorMapping addRelationshipMappingWithSourceKeyPath:@"productPictureURLs" mapping:productPictureStringMapping];


    RKObjectMapping *getProductPageMapping = [RKObjectMapping mappingForClass:[TransferProductPage class]];
    [getProductPageMapping addAttributeMappingsFromArray:@[@"productID", @"brandID", @"productName", @"descriptionBody"]];


    RKObjectMapping *stringMapping = [RKObjectMapping mappingForClass:[TransferObjectString class]];
    [stringMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"string"]];
    [getProductPageMapping addRelationshipMappingWithSourceKeyPath:@"colors" mapping:stringMapping];
    [getProductPageMapping addRelationshipMappingWithSourceKeyPath:@"sizes" mapping:stringMapping];
    [getProductPageMapping addRelationshipMappingWithSourceKeyPath:@"productPictureURLs" mapping:stringMapping];
    [getProductPageMapping addRelationshipMappingWithSourceKeyPath:@"configurationsAvailable" mapping:configurationForSizeAndColorMapping];



    return getProductPageMapping;

}

但是,当我请求对象映射失败,并给我这个错误信息:>

    `No mappable object representations were found at the key paths searched."`  UserInfo={NSLocalizedDescription=No mappable object representations were found at the key paths searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: 
    The representation inputted to the mapper was found to contain nested object representations at the following key paths: brandID, colors, configurationsAvailable, descriptionBody, productID, productName, productPictureURLs, sizes
    This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null

`

我的问题是,我不知道映射字符串数组和配置数组的最佳方式。 我知道你可以AFNetworking将JSON反序列化为数组。 但在这种情况下,就我所见,这将导致多个请求,我不知道如何映射这些请求。

我的工作是制作一个只存储字符串的自定义对象,并使用零键路径将多个值映射到键。

如何使用Restkit将复杂对象中的字符串和自定义对象的数组映射到一起?

我使用xcode 7.3 restkit 0.26

编辑:这是我创建描述符并发送请求的地方:

     NSURL *baseURL = [NSURL URLWithString:@"http://example.ngrok.io"];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

// Initialize the object manager
    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];


    // Descriptor
    RKResponseDescriptor* getProductPageResponse =
    [RKResponseDescriptor responseDescriptorWithMapping:[self getProductMapping]
                                                 method:RKRequestMethodGET
                                            pathPattern:@"/thewebappv2/productController/productpage"
                                                keyPath:nil
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


    [objectManager addResponseDescriptor:getProductPageResponse];





    int64_t prodcutID = 90;

    [objectManager getObjectsAtPath:[NSString stringWithFormat:@"/thewebappv2/productController/productpage/%lld", prodcutID]
                                           parameters:nil
                                              success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                                                  // Assign the values you get from the TransferProductpage to the Product property
                                                  // the Strings are wrapped in custom Objects so they can be deserialized using Restkit



                                              }
                                              failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                  NSLog(@"The Prodcut could not be downlaoded:': %@", error.localizedDescription);


                                              }];

更新:除了@wain修复之外,你必须改变你映射到的类的类型为对象(例如NSNumber)而不是像int_64这样的基元。


更改路径模式以包含完整内容:

@"/thewebappv2/productController/productpage/:id"
链接地址: http://www.djcxy.com/p/49203.html

上一篇: No response descriptors match the response loaded

下一篇: iOS Restkit: how to parse nested path