如何在 Gson 序列化中保持字段序列
2022-09-01 00:05:31
似乎生成具有对象的随机分布字段的JSON代码。有没有办法以某种方式修复字段顺序?Gson.toJson(Object object)
public class Foo {
public String bar;
public String baz;
public Foo( String bar, String baz ) {
this.bar = bar;
this.baz = baz;
}
}
Gson gson = new Gson();
String jsonRequest = gson.toJson(new Foo("bar","baz"));
字符串 jsonRequest 可以是:
-
{ "bar":"bar", "baz":"baz" }
(正确) -
{ "baz":"baz", "bar":"bar" }
(顺序错误)