JSON 字符串到 Java 对象 JSON 字符串键重命名
JSON String to Java object with JSON string key rename
我有以下要求将 JSON 字符串转换为 Java 对象。
class Person {
private String firstName;
private String lastName;
}
ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
Person person = MAPPER.readValue(jsonString, Person.class);
上面的转换 returns null 因为 Person class 属性名称不匹配。
使用 @JsonProperty 它可以正确转换,但最终的 JSON 结果键与 jsonString 中的键相同。
{
"FST_NME" : "stack",
"LST_NME" : "overflow"
}
但我正在寻找类似下面的内容。
{
"firstName" : "stack",
"lastName" : "overflow"
}
我尝试重命名 jsonString 中的密钥,它按预期工作。
但是我们可以使用任何注释或任何其他方法来实现上述结果吗?
谢谢。
在获取方法上添加以下注释。
@JsonGetter("FST_NME")
public String getFirstName(){
return first Name;
}
您只需要在 setter 和 getter 中添加 @JsonProperty
。
在你的情况下,
您正在阅读 JSON 字符串键 FST_NME
,因此您需要在 firstName
的 setter 方法中添加 @JsonProperty('FST_NME')
并且您想要获得最终的 JSON string with key firstName
所以你需要在 firstName
的 getter 方法中添加 @JsonProperty('firstName')
。
lastName
.
也一样
以下是工作代码。
package com.ubaid.Whosebug;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Saravanan {
@SneakyThrows
public static void main(String[] args) {
ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
Person person = MAPPER.readValue(jsonString, Person.class);
String finalJson = MAPPER.writeValueAsString(person);
log.debug("Final JSON: {}", finalJson);
}
}
class Person {
private String firstName;
private String lastName;
@JsonProperty("firstName")
public String getFirstName() {
return firstName;
}
@JsonProperty("FST_NME")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@JsonProperty("lastName")
public String getLastName() {
return lastName;
}
@JsonProperty("LST_NME")
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
以上代码的输出为:
Final JSON: {"firstName":"stack","lastName":"OVERFLOW"}
- 使用@JsonProperty
将数据读取到 DTO class(Person DTO)
- Class随心所欲
- 将 DTO 转换为 Person
class PersonDTO {
@JsonProperty(value = "FST_NME")
private String firstName;
@JsonProperty(value = "LST_NME")
private String lastName;
}
class Person {
private String firstName;
private String lastName;
}
ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
PersonDTO persondto = MAPPER.readValue(jsonString, PersonDTO.class);
Person person = new Person();
person.setFirstName(persondto.getFirstName());
person.setLastName(persondto.getLastName());
我有以下要求将 JSON 字符串转换为 Java 对象。
class Person {
private String firstName;
private String lastName;
}
ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
Person person = MAPPER.readValue(jsonString, Person.class);
上面的转换 returns null 因为 Person class 属性名称不匹配。
使用 @JsonProperty 它可以正确转换,但最终的 JSON 结果键与 jsonString 中的键相同。
{
"FST_NME" : "stack",
"LST_NME" : "overflow"
}
但我正在寻找类似下面的内容。
{
"firstName" : "stack",
"lastName" : "overflow"
}
我尝试重命名 jsonString 中的密钥,它按预期工作。
但是我们可以使用任何注释或任何其他方法来实现上述结果吗?
谢谢。
在获取方法上添加以下注释。
@JsonGetter("FST_NME")
public String getFirstName(){
return first Name;
}
您只需要在 setter 和 getter 中添加 @JsonProperty
。
在你的情况下,
您正在阅读 JSON 字符串键 FST_NME
,因此您需要在 firstName
的 setter 方法中添加 @JsonProperty('FST_NME')
并且您想要获得最终的 JSON string with key firstName
所以你需要在 firstName
的 getter 方法中添加 @JsonProperty('firstName')
。
lastName
.
以下是工作代码。
package com.ubaid.Whosebug;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Saravanan {
@SneakyThrows
public static void main(String[] args) {
ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
Person person = MAPPER.readValue(jsonString, Person.class);
String finalJson = MAPPER.writeValueAsString(person);
log.debug("Final JSON: {}", finalJson);
}
}
class Person {
private String firstName;
private String lastName;
@JsonProperty("firstName")
public String getFirstName() {
return firstName;
}
@JsonProperty("FST_NME")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@JsonProperty("lastName")
public String getLastName() {
return lastName;
}
@JsonProperty("LST_NME")
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
以上代码的输出为:
Final JSON: {"firstName":"stack","lastName":"OVERFLOW"}
- 使用@JsonProperty 将数据读取到 DTO class(Person DTO)
- Class随心所欲
- 将 DTO 转换为 Person
class PersonDTO {
@JsonProperty(value = "FST_NME")
private String firstName;
@JsonProperty(value = "LST_NME")
private String lastName;
}
class Person {
private String firstName;
private String lastName;
}
ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
PersonDTO persondto = MAPPER.readValue(jsonString, PersonDTO.class);
Person person = new Person();
person.setFirstName(persondto.getFirstName());
person.setLastName(persondto.getLastName());