GSon命名策略不适用于Java Map

时间:2016-07-06 06:44:58

标签: java hashmap gson

我创建了一个java哈希映射,并尝试使用gson对其进行序列化。我为此序列化使用了命名策略。序列化是成功的,但命名策略不起作用。

GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES);
Gson gson = builder.create();

Map<String, String> resultData = new HashMap<>();
resultData.put("firstName","Kevin");
resultData.put("homeAddress","20/b,Colombo.");
resultData.put("passportNumber","1231239997M");

gson.toJson(resultData);

我想要

{"First Name":"Kevin","Home Address":"20/b,Colombo.","Passport Number":"1231239997M",}

结果。

但总是给出

{"firstName":"Kevin","homeAddress":"20/b,Colombo.","passportNumber":"1231239997M"}

请让我知道我该怎么做。

提前致谢。

0 个答案:

没有答案