Reputation: 657
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.
Upvotes: 28
Views: 19835
Reputation: 2224
Use the annotation @SerializedName("name")
on your fields. Like this:
Object A
@SerializedName("f1")
String field1 = "abc";
@SerializedName("f2")
String field2 = "xyz";
See https://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html.
Upvotes: 46