我尝试解析从 REST-Webservice 获得的 JSON。
座位.json:
{"seat":{ "row":1,
"seatNr":1,
"seatId":5782}}
我的 MTLModel (这不起作用。因为 json 字段前面有一个座位。)
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"seatId" :@"seatId",
@"row" :@"row",
@"seatNr" :@"seatNr"};
}
这将起作用,因为它通过座位字典访问字段。
+ (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];
}
这不起作用,因为座位映射仅在字典以座位开头时才有效。
如何将 Mantle SDK 与这样的 JSON 对象一起使用?