Mantle - MTL JSON为同一属性序列化两个值

时间:2014-03-07 22:48:54

标签: ios github-mantle

我调用一个api,它可以为相同的值“id”设置不同的接口:“id”,“_ id”或“pId”。

但是目前,它仅适用于第一个:@“id”:@“_ id”。另一个被忽略了。

JSON

 object:{ "pId" : 192039,
         "name" : "test}

IOS工作原理:self.id = 192039

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"id": @"pId",
             @"id": @"_id",
             @"id": @"id",
             @"title": @"name",
            };
}

IOS不工作:self.id =

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{

             @"id": @"_id",
             @"id": @"id",
             @"id": @"pId",
             @"title": @"name",
            };
}

修改

字典中的键必须是唯一的....

我找到的唯一解决方案是创建3个不同的属性并覆盖这样的setter:

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"pId": @"pId",
             @"id2": @"_id",
             @"id": @"id",
             @"title": @"name",
            };
}

- (void) setPid:(NSString *)pId
{
    _id = pId;
}

- (void) setId2:(NSString *)id2
{
    _id = id2;
}

2 个答案:

答案 0 :(得分:0)

你不能重复这个键

@{key:value,...}

你可以尝试其他这样的事情

return @{
     @"id" : @[@"_id",@"id",@"pId"],
     @"title" : @"name"
   };

或在@“id”

等@“id”键中添加前缀

答案 1 :(得分:0)

字典中的键必须是唯一的....

我找到的唯一解决方案是创建3个不同的属性并覆盖这样的setter:

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"pId": @"pId",
             @"id2": @"_id",
             @"id": @"id",
             @"title": @"name",
            };
}

- (void) setPid:(NSString *)pId
{
    _id = pId;
}

- (void) setId2:(NSString *)id2
{
    _id = id2;
}
相关问题