Say I have an Object:
Object A
String field1 = "abc";
String field2 = "xyz";
The json for the above is:
{
"ObjectA": {
"field1": "abc",
"field2": "xyz"
}
}
I was trying to create a new id for the field names before sending the json. E.g. "field1" to be called "f1" and "field2" to be called "f2". So the intended output json is shown below:
{
"ObjectA": {
"f1": "abc",
"f2": "xyz"
}
}
I am not sure how to do this. Can the above be done in a clean way? Thanks for your help and pointers.
I am using gson.
解决方案
Use the annotation @SerializedName("name") on your fields. Like this:
Object A
@SerializedName("f1")
String field1 = "abc";
@SerializedName("f2")
String field2 = "xyz";