Need your help on conversion of java objects to json. current the json result showing all the key in small letter case, i need it to be upper case.
需要你的帮助将java对象转换为json。当前json结果显示小写字母的所有键,我需要它是大写。
ObjectMapper mapper = new ObjectMapper();
Writer strWriter = new StringWriter();
mapper.writeValue(strWriter, obj);
String jsonString= strWriter.toString();
and the result is
结果是
[{"flags":"1","name":"Peter","location":"London","startDate":"2012-01-06 00:00"}]
but i want results like this (all key key value should be in UPPER CASE):
但我想要这样的结果(所有关键键值应该是大写的):
[{"FLAGS":"YU","NAME":"Peter","LOCATION":"London","STARTDATE":"2012-01-06 00:00"}]
and also is it possible to get like this also (key first letter in upper case):
并且也可以这样(大写的关键第一个字母):
[{"Flags":"1","Name":"Peter","Location":"London","StartDate":"2012-01-06 00:00"}]
Can anyone help me on this.
谁可以帮我这个事。
Thanks in advance.
提前致谢。
2 个解决方案
#1
13
There are multiple ways to do it with Jackson.
杰克逊有多种方法可以做到这一点。
Annotations
You could annotate your object with @JsonProperty
annotations on your fields or on your getter methods.
您可以使用字段或getter方法上的@JsonProperty注释来注释对象。
Example:
@JsonProperty("Name")
public final String name;
@JsonProperty("Location")
public String getLocation() {
return location;
}
Implement JsonSerializableWithType interface
@Override
public void serialize(final JsonGenerator jG, final SerializerProvider p)
throws IOException, JsonProcessingException
{
serializeWithType(jG, p, null);
}
@Override
public void serializeWithType(final JsonGenerator jG, final SerializerProvider p, final TypeSerializer typeSer)
throws IOException, JsonProcessingException
{
// here you can do your own serialization
}
#2
0
I would advice to use the @JsonNaming
annotation on class level.
我建议在类级别使用@JsonNaming注释。
Yet afaik there is no strategy out there to fit your needs for total uppercase. But you can probably just write your own.
然而,afaik没有任何策略可以满足您对大写字母的需求。但你可以写自己的。
In my case I needed first character uppercase. So I used the existing @JsonNaming(value = UpperCamelCaseStrategy.class)
.
在我的情况下,我需要第一个字符大写。所以我使用了现有的@JsonNaming(value = UpperCamelCaseStrategy.class)。
#1
13
There are multiple ways to do it with Jackson.
杰克逊有多种方法可以做到这一点。
Annotations
You could annotate your object with @JsonProperty
annotations on your fields or on your getter methods.
您可以使用字段或getter方法上的@JsonProperty注释来注释对象。
Example:
@JsonProperty("Name")
public final String name;
@JsonProperty("Location")
public String getLocation() {
return location;
}
Implement JsonSerializableWithType interface
@Override
public void serialize(final JsonGenerator jG, final SerializerProvider p)
throws IOException, JsonProcessingException
{
serializeWithType(jG, p, null);
}
@Override
public void serializeWithType(final JsonGenerator jG, final SerializerProvider p, final TypeSerializer typeSer)
throws IOException, JsonProcessingException
{
// here you can do your own serialization
}
#2
0
I would advice to use the @JsonNaming
annotation on class level.
我建议在类级别使用@JsonNaming注释。
Yet afaik there is no strategy out there to fit your needs for total uppercase. But you can probably just write your own.
然而,afaik没有任何策略可以满足您对大写字母的需求。但你可以写自己的。
In my case I needed first character uppercase. So I used the existing @JsonNaming(value = UpperCamelCaseStrategy.class)
.
在我的情况下,我需要第一个字符大写。所以我使用了现有的@JsonNaming(value = UpperCamelCaseStrategy.class)。