Jackson:Serialise ArrayList不工作​​?

问题描述:

我指的是这个例子here序列化我的对象。Jackson:Serialise ArrayList不工作​​?

我有这个最初,它的工作原理。

public class MyClass implements Serializable { 
    private String mediaitem_id; 
    private String customer_id; 
    private int quantity; 

    public MyClass(String item, String customer, int quantity){ 
     this.mediaitem_id = item; 
     this.customer_id = customer; 
     this.quantity = quantity;  
    } 

    public String toJson(){ 
     ObjectMapper mapper = new ObjectMapper(); 

     try{ 
      mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE); 
      return mapper.writeValueAsString(this); 
     }catch(Exception ex){ 
      log.error("Error converting MyClass to json " + this, ex); 
     } 

     return ""; 
    } 
} 

MyClass myClass = new MyClass("1234", "23234", 5); 

myClass.toJson()给出了下面的,这正是我想要的:

{ mediaitem_id: '1234', customer_id: '23234', quantity: 5 } 

但现在我需要到一个ArrayList添加到类,需要连载一样好,所以我添加一个新类账户:

public static class Account implements Serializable { 
    public String accountname; 
    public String accountid; 

    public Account(String accountname, String accountid) { 
     this.accountname = accountname; 
     this.accountid = accountid; 
    } 
} 

public class MyClass implements Serializable { 
    private String mediaitem_id; 
    private String customer_id; 
    private int quantity; 
    private List<Account> accounts = new ArrayList<>(); 

    public MyClass(String item, String customer, int quantity){ 
     this.mediaitem_id = item; 
     this.customer_id = customer; 
     this.quantity = quantity;  
    } 

    public void addAccount(String accountname, String accountid) { 
     Account anAccount = new Account(accountname, accountid); 
     accounts.add(anAccount); 
    }  

    public String toJson(){ 
     ObjectMapper mapper = new ObjectMapper(); 

     try{ 
      mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE); 
      return mapper.writeValueAsString(this); 
     }catch(Exception ex){ 
      log.error("Error converting MyClass to json " + this, ex); 
     } 

     return ""; 
    } 
} 

MyClass myClass = new MyClass("1234", "23234", 5); 
myClass.addAccount("acc-01", "a001"); 
myClass.addAccount("acc-02", "a002"); 

myClass.toJson()仍然给出了同样的:

{ mediaitem_id: '1234', customer_id: '23234', quantity: 5 } 

我现在想念什么?

我想要得到的东西,如:

{ mediaitem_id: '1234', customer_id: '23234', quantity: 5, accounts: [{accountname: 'acc-01', accountid: 'a001'}, {accountname: 'acc-02', accountid: 'a002'}]} 
+0

感觉就像你遗漏了一些细节。你可以发布[mcve]吗? – shmosel

我推荐添加getter和setter为您MyClass所有财产。

public String getMediaitem_id() { 
     return mediaitem_id; 
    } 

    public void setMediaitem_id(String mediaitem_id) { 
     this.mediaitem_id = mediaitem_id; 
    } 

    public String getCustomer_id() { 
     return customer_id; 
    } 

    public void setCustomer_id(String customer_id) { 
     this.customer_id = customer_id; 
    } 

    public int getQuantity() { 
     return quantity; 
    } 

    public void setQuantity(int quantity) { 
     this.quantity = quantity; 
    } 

    public List<Account> getAccounts() { 
     return accounts; 
    } 

    public void setAccounts(List<Account> accounts) { 
     this.accounts = accounts; 
    } 
+0

你是对的@mrtasin,我把清单帐户的getter和setter,它现在工作! –