我尝试解析从REST-Webservice获得的JSON。
Seat.json:
{"seat":{ "row":1,
"seatNr":1,
"seatId":5782}}我的MTLModel (这不起作用。因为在json字段前面有一个座位。)
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"seatId" :@"seatId",
@"row" :@"row",
@"seatNr" :@"seatNr"};
}这将会起作用,因为它通过seat字典访问字段。
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"seatId" :@"seat.seatId",
@"row" :@"seat.row",
@"seatNr" :@"seat.seatNr"};
}但是这样嵌套的对象就不能工作了。JSON示例:
{"participant": {"name":"Test User",
"participantId":4243,
"chosenSeat":{"row":1,
"seatNr":21,
"seatId":5802}
}映射:
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"name" : @"participant.name",
@"participantId" : @"participant.participantId",
@"seat" : @"participant.chosenSeat"};
}
+ (NSValueTransformer *)seatJSONTransformer {
return [MTLJSONAdapter dictionaryTransformerWithModelClass:Seat.class];
}这不会起作用,因为只有当字典以seat开头时,seat映射才会起作用。
如何将Mantle SDK与这样的JSON对象一起使用?
发布于 2016-05-29 16:43:11
似乎seat不是使用Mantle初始化对象所需的数据的一部分,因此我将使用:
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"seatId" :@"seatId",
@"row" :@"row",
@"seatNr" :@"seatNr"};
}(或mtl_identityPropertyMapWithModel)
然后,在解析JSON时,只需使用:
NSError *error = nil;
Seat *seat = [MTLJSONAdapter modelOfClass:Seat.class
fromJSONDictionary:sourceJSON[@"seat"] error:&error];https://stackoverflow.com/questions/33302136
复制相似问题