【问题标题】:RestKit - Not key-value coding compliant, no root in JSONRestKit - 不符合键值编码,在 JSON 中没有根
【发布时间】:2013-03-23 21:21:47
【问题描述】:

我一直在尝试从 JSON 响应以及我在控制台输出中看到的所有内容中映射以下对象,没有任何原因导致映射不成功 - 如果有人可以检查一下,我将不胜感激见:

@interface RKElectionsModel : NSObject

@property (nonatomic, assign) bool isActive;
@property (nonatomic, strong) NSNumber *electionID;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *summary;
@property (nonatomic, strong) NSNumber *availableSeats;
@property (nonatomic, strong) NSNumber *candidatesCount;
@property (nonatomic, strong) NSNumber *withdrawnCount;
@property (nonatomic, strong) NSSet *candidates;

@end

/**
 * Election Detail Mapping: Getting all election details, we have some extra information from
 * the API call
 *
*/
RKObjectMapping *electionDetailsMapping = [RKObjectMapping mappingForClass:[RKElectionsModel class]];

// Map JSON -> entities
[electionDetailsMapping addAttributeMappingsFromDictionary:@{
     @"id": @"electionID",
     @"title": @"title",
     @"summary": @"summary",
     @"active": @"isActive",
     @"availableSeats": @"availableSeats",
     @"candidatesCount": @"candidatesCount",
     @"withdrawnCount": @"withdrawnCount"
 }];

// Register our mappings with the provider
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:electionDetailsMapping pathPattern:@"/api/elections/:electionID/all" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

pastebin 上的Console output 以及您可以访问here 的任何 JSON 响应示例

感谢任何帮助, 刘易斯

【问题讨论】:

    标签: ios objective-c json restkit key-value-coding


    【解决方案1】:

    似乎您试图将 NSString 映射到 NSNumber。

    在您的NSObject 中尝试添加以下验证,并在映射开始时将您从服务器收到的字符串转换为 NSNumber。

    - (BOOL)validateAvailableSeats:(id *)ioValue error:(NSError **)outError {
           // Force the value to be a NSNumber
           *ioValue = [NSNumber numberWithDouble:[(NSString*)value doubleValue] ]
           return YES;
    }
    

    RestKit Wiki 中,您会发现更多值的转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 2013-03-17
      相关资源
      最近更新 更多