YYModel icon indicating copy to clipboard operation
YYModel copied to clipboard

模型转字典出现问题,模型中属性名变成JSON 中的Key

Open medivh-xiong opened this issue 8 years ago • 3 comments

我先是使用字典转模型,中间因为模型中的属性名和JSON中的key不一致,进行转化。拿到模型后,再把模型转字典,这时候发现得到的字典里面的属性名变成JSON中的key而不是我模型中正确的属性名。通过断点,发现问题出现在: - (id)yy_modelToJSONObject { /* Apple said: The top level object is an NSArray or NSDictionary. All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull. All dictionary keys are instances of NSString. Numbers are not NaN or infinity. */ id jsonObject = ModelToJSONObjectRecursive(self); if ([jsonObject isKindOfClass:[NSArray class]]) return jsonObject; if ([jsonObject isKindOfClass:[NSDictionary class]]) return jsonObject; return nil; } 在这个方法中传过去的self中属性名是正确的,在 id jsonObject = ModelToJSONObjectRecursive(self);之后,拿到的jsonObject中属性名就变成了之前JSON中的key值

medivh-xiong avatar Jan 14 '17 08:01 medivh-xiong

解决了么

miaoruiyuan avatar Jun 28 '18 10:06 miaoruiyuan

@medivh-xiong

miaoruiyuan avatar Jun 28 '18 10:06 miaoruiyuan

ModelToJSONObjectRecursive 方法里面改一行代码就可以了,1270行 if (!dic[propertyMeta->_name]) { [dic setValue:value forKey:propertyMeta->_name]; NSLog(@"========%@",dic);

        }

SamLee123 avatar Nov 06 '18 08:11 SamLee123