我有一个 JSON 字符串需要转换为 JAVA 对象。我需要将一些字段包装到不同的 JAVA 类中。我面临的问题是我无法包装它,并且我将 Java 字段设为 null。 请看下面的JSON
{
"first_name": "John",
"last_name": "DCosta",
"age": "29",
"phone": "+173341238",
"address_line_1": "43 Park Street",
"address_line_2": "Behind C21 Mall",
"city": "Cario",
"country": "UK",
"child1": {
"name": "Peter",
"age": "5"
},
"child2": {
"name": "Paddy",
"age": "2"
},
"child3": {
"name": "Premus",
"age": "1"
}
}
请看下面我的 JAVA 类 -
Details.java
public class Details {
private Person person;
private Address address;
private Child[] children;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Child[] getChildren() {
return children;
}
public void setChildren(Child[] children) {
this.children = children;
}
}
Person.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person {
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@JsonProperty("age")
private Integer age;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Address.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class Address {
@JsonProperty("phone")
private String phone;
@JsonProperty("address_line_1")
private String addressLine1;
@JsonProperty("address_line_2")
private String addressLine2;
@JsonProperty("city")
private String city;
@JsonProperty("country")
private String country;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public String getAddressLine2() {
return addressLine2;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Child.java
public class Child {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
我的将 JSON 转换为 JAVA 对象的代码 -
String filePath = "test.json";
File file = new File(filePath);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Details details = mapper.readValue(file, Details.class);
System.out.println(details.getPerson());
我面临的问题是我得到详细信息对象中的所有值都是null。如果我删除 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 然后我得到以下异常
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "first_name" (class learn.springboot.model.Details), not marked as ignorable (3 known properties: "address", "person", "children"])
at [Source: (File); line: 2, column: 17] (through reference chain: learn.springboot.model.Details["first_name"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.ja
答案 0 :(得分:1)
我认为不可能有一些包装类并期望 Jackson 从包装类中扁平化和提取所有这些字段并将扁平的 JSON 字段映射到它们。
基于 Details
类,Jackson 希望有一个像下面这样的 JSON 对象(省略了内部字段):
{
"person": {},
"address": {},
"children": []
}
因此,您必须将 Details
类更改为如下所示:
public class Details {
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@JsonProperty("age")
private Integer age;
...
}