我想使用Jackson来反序列化以下json结构
{
"name" : "Alex",
"favoriteNumbers" : ["42", "9"]
}
将放入下面的类结构
public class Person {
private String name;
private ListOfNumbers favoriteNumbers;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ListOfNumbers getFavoriteNumbers() {
return favoriteNumbers;
}
public void setFavoriteNumbers(ListOfNumbers favoriteNumbers) {
this.favoriteNumbers = favoriteNumbers;
}
}
class ListOfNumbers {
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
如果我在Person上使用@JsonUnwrapped。favoriteNumbers,我只能反序列化以下内容:
{
"name" : "Alex",
"list" : ["42", "9"]
}
是否有一个简单的方法来完成这一点?我不想添加@JsonProperty到ListOfNumbers。列表,因为我需要根据引用ListOfNumbers的字段使用不同的名称。例如,具有ListOfNumbers的类Company应该能够反序列化以下内容:
{
"companyName" : "MyCompany",
"phoneNumbers" : ["42343243", "234235239"]
}
@JsonUnwrapped
用于在序列化过程中展开对象的值。所以使用这个注释将使属性favoriteNumbers
->List
。就像你的帖子一样。所以简单的方法是:
class ListOfNumbers {
//private List<String> list; ->//private List<String> favoriteNumbers;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
您可以在同一个类中直接使用@JsonProperty和一个额外的方法。这不能解决你的问题吗?
tl,博士
@JsonIgnore
private ListOfNumbers companyNumbers;
@JsonProperty("phoneNumbers")
public List<String> getPhoneNumbers() {
return companyNumbers.getList();
}
@JsonProperty("phoneNumbers")
public void setPhoneNumbers(List<String> phoneNumbers) {
this.companyNumbers = new ListOfNumbers(phoneNumbers);
}
完整的示例
package org.json.example;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
public class App {
public static class Person {
private String name;
@JsonIgnore
private ListOfNumbers favoriteNumbers;
public Person() {
favoriteNumbers = new ListOfNumbers(new ArrayList<>());
}
@JsonProperty("favoriteNumbers")
public List<String> getPhoneNumbers() {
return favoriteNumbers.getList();
}
@JsonProperty("favoriteNumbers")
public void setPhoneNumbers(List<String> phoneNumbers) {
this.favoriteNumbers = new ListOfNumbers(phoneNumbers);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ListOfNumbers getFavoriteNumbers() {
return favoriteNumbers;
}
public void setFavoriteNumbers(ListOfNumbers favoriteNumbers) {
this.favoriteNumbers = favoriteNumbers;
}
}
public static class Company {
private String name;
@JsonIgnore
private ListOfNumbers companyNumbers;
public Company() {
companyNumbers = new ListOfNumbers(new ArrayList<>());
}
@JsonProperty("phoneNumbers")
public List<String> getPhoneNumbers() {
return companyNumbers.getList();
}
@JsonProperty("phoneNumbers")
public void setPhoneNumbers(List<String> phoneNumbers) {
this.companyNumbers = new ListOfNumbers(phoneNumbers);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ListOfNumbers getCompanyNumbers() {
return companyNumbers;
}
public void setCompanyNumbers(ListOfNumbers companyNumbers) {
this.companyNumbers = companyNumbers;
}
}
public static class ListOfNumbers {
private List<String> list;
ListOfNumbers(List<String> list) {
this.list = list;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
public static void main(String[] args) throws JsonProcessingException {
doPersonSample();
doCompanySample();
}
private static void doCompanySample() throws JsonProcessingException {
var mapper = new ObjectMapper();
var person = new Company();
person.name = "stavang Ltd.";
person.getCompanyNumbers().getList().add("26");
person.getCompanyNumbers().getList().add("4");
var toJson = mapper.writeValueAsString(person);
System.out.println(toJson);
var fromJson = "{n" +
" "name" : "stavang Ltd.",n" +
" "phoneNumbers" : ["26", "4"]n" +
"}";
var deserializedCompany = mapper.readValue(fromJson, Company.class);
System.out.println(deserializedCompany.getCompanyNumbers().getList());
}
private static void doPersonSample() throws JsonProcessingException {
var mapper = new ObjectMapper();
var person = new Person();
person.name = "Alex";
person.getFavoriteNumbers().getList().add("42");
person.getFavoriteNumbers().getList().add("9");
var toJson = mapper.writeValueAsString(person);
System.out.println(toJson);
var fromJson = "{n" +
" "name" : "Alex",n" +
" "favoriteNumbers" : ["42", "9"]n" +
"}";
var deserializedPerson = mapper.readValue(fromJson, Person.class);
System.out.println(deserializedPerson.favoriteNumbers.getList());
}
}