I need to serialize a list of simple Java objects to JSON using Google Gson library.
The object:
public class SimpleNode {
private String imageIndex;
private String text;
public String getImageIndex() {
return imageIndex;
}
public void setImageIndex(String imageIndex) {
this.imageIndex = imageIndex;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
I have written the following code for serialization:
List<SimpleNode> testNodes = repository.getElements(0);
Gson gson = new Gson();
String jsonNodesAsString = gson.toJson(testNodes);
It works, but the field name of the JSON objects is lowerCamelCase. Like this:
[
{
"imageIndex": "1",
"text": "Text 1"
},
{
"imageIndex": "2",
"text": "Text 2"
}
]
How do I get a JSON with UpperCamelCase field name, like this:
[
{
"ImageIndex": "1",
"Text": "Text 1"
},
{
"ImageIndex": "2",
"Text": "Text 2"
}
]
I think that I can rename member variables in to UpperCamelCase, but may be there is another way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…