英文:
Gson: skip node level for serialization
问题
I have some json which contains fields with custom names, e.g.:
{
"user_id": 123,
"user_name": "John",
"field_with_custom_name_1": "value1",
"field_with_custom_name_2": "value2",
"field_with_custom_name_3": "value3"
}
For describe this json the next model was created:
public class UserData {
@SerializedName("user_id")
private int userId;
@SerializedName("user_name")
private String userName;
private Map<String, String> customFields;
}
But after serialization we have json with the next structure:
{
"user_id": 123,
"user_name": "John",
"customFields": {
"field_with_custom_name_1": "value1",
"field_with_custom_name_2": "value2",
"field_with_custom_name_3": "value3"
}
}
Can you suggest how to ignore "customFields" level in the result?
英文:
I have some json which contains fields with custom names, e.g.:
{
"user_id": 123,
"user_name": "John",
"field_with_custom_name_1": "value1",
"field_with_custom_name_2": "value2",
"field_with_custom_name_3": "value3"
}
For describe this json the next model was created:
public class UserData {
@SerializedName("user_id")
private int userId;
@SerializedName("user_name")
private String userName;
private Map<String, String> customFields;
}
But after serialization we have json with the next structure:
{
"user_id": 123,
"user_name": "John",
"customFields": {
"field_with_custom_name_1": "value1",
"field_with_custom_name_2": "value2",
"field_with_custom_name_3": "value3"
}
}
Can you suggest how to ignore "customFields" level in the result?
答案1
得分: 0
你可以使用以下的注解来将一个字段从序列化和反序列化中排除:
@Expose(serialize = false, deserialize = false)
英文:
You can use the following annotation to exclude a field from serialization and deserialization:
@Expose (serialize = false, deserialize = false)
专注分享java语言的经验与见解,让所有开发者获益!
评论