2016-08-26 31 views
0

我的下一個類有相應的JSON機構:如何在JSON體中展開和對象?

public class MyResponse { 

    private String meta; 
    private String info; 

    private String respOne; 
    //... 
} 

而且

public class MyResponseNew { 

    private String meta; 
    private String info; 

    private String respNew; 
    private String respbBest; 

    //... 
} 

我需要JSONs:

{ 
    "meta": "", 
    "info": "", 

    "respOne": "" 
} 

而且

{ 
    "meta": "", 
    "info": "", 

    "respNew": "", 
    "respbBest": "" 
} 

所以,我想提取一般信息一類,並有這樣的事情:

public class GeneralSubResponse { 
    private String meta; 
    private String info; 

    //.. 

} 

public class MyResponse { 

    /*@JsonExpanded*/ 
    private GeneralSubResponse generalInfo; 

    private String respOne; 

    //... 

} 

字符串類型 - 只爲例子,可以有任意嵌套的任何對象...

是否有可能使用Jackson lib?或者還有其他方式可以做這樣的事嗎?主要關心的是不要爲每個對象重複代碼。

選項1:

+0

的可能的複製[?是傑克遜的@JsonSubTypes仍然有必要對多態反序列化(http://stackoverflow.com/questions/31665620/is-jacksons-jsonsubtypes-still-necessary-for -polymorphic-反序列化) –

回答

1

你可以用普通的類繼承或將其添加爲解開對象做到這一點與Java繼承:

public class MyResponse extends GeneralSubResponse { 

    private String respOne; 
    //... 
} 

public class MyResponseNew extends GeneralSubResponse{ 

    private String respNew; 
    private String respbBest; 

} 

選項2:JsonUnwrapped對象作爲對象屬性:

public class MyResponseUnwrapped{ 

    @JsonUnwrapped 
    private GeneralSubResponse subResponse; 

    private String respOne; 
} 

public class MyResponseNewUnwrapped{ 

    @JsonUnwrapped 
    private GeneralSubResponse subResponse; 

    private String respNew; 
    private String respbBest; 
} 

測試(兩種選擇):

public class Test { 

    public static String getJsonString(Object o){ 
     ObjectMapper mapper = new ObjectMapper(); 

     //For testing 
     try { 
      //Convert object to JSON string 
      String jsonInString = mapper.writeValueAsString(o); 
      //System.out.println(jsonInString); 
      return jsonInString; 


     } catch (JsonProcessingException e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public static void main(String args[]){ 
     MyResponse myResponse = new MyResponse(); 
     myResponse.setInfo("info"); 
     myResponse.setMeta("meta"); 
     myResponse.setRespOne("respOne"); 
     System.out.println(myResponse.getClass().getSimpleName() + " = " + Test.getJsonString(myResponse)); 
     System.out.println("------------------------------"); 
     MyResponseNew myResponseNew = new MyResponseNew(); 
     myResponseNew.setInfo("infoNew"); 
     myResponseNew.setMeta("metaNew"); 
     myResponseNew.setRespbBest("respBest"); 
     myResponseNew.setRespNew("respNew"); 
     System.out.println(myResponseNew.getClass().getSimpleName() + " = " + Test.getJsonString(myResponseNew)); 
     System.out.println("------------------------------"); 
     MyResponseUnwrapped myResponseUnwrapped = new MyResponseUnwrapped(); 
     GeneralSubResponse subResponse = new GeneralSubResponse(); 
     subResponse.setInfo("infoUnwrapped"); 
     subResponse.setMeta("metaUnwrapped"); 
     myResponseUnwrapped.setSubResponse(subResponse); 
     myResponseUnwrapped.setRespOne("respTwo"); 
     System.out.println(myResponseUnwrapped.getClass().getSimpleName() + " = " + Test.getJsonString(myResponseUnwrapped)); 
     System.out.println("------------------------------"); 
     MyResponseNewUnwrapped myResponseNewUnwrapped = new MyResponseNewUnwrapped(); 
     GeneralSubResponse subResponse2 = new GeneralSubResponse(); 
     subResponse2.setInfo("infoNewUnwrapped"); 
     subResponse2.setMeta("metaNewUnwrapped"); 
     myResponseNewUnwrapped.setSubResponse(subResponse2); 
     myResponseNewUnwrapped.setRespbBest("respBestUnwrapped"); 
     myResponseNewUnwrapped.setRespNew("respNewUnwrapped"); 
     System.out.println(myResponseNewUnwrapped.getClass().getSimpleName() + " = " + Test.getJsonString(myResponseNewUnwrapped)); 
    } 

} 

結果:

MyResponse = {"meta":"meta","info":"info","respOne":"respOne"} 
------------------------------ 
MyResponseNew = {"meta":"metaNew","info":"infoNew","respNew":"respNew","respbBest":"respBest"} 
------------------------------ 
MyResponseUnwrapped = {"meta":"metaUnwrapped","info":"infoUnwrapped","respOne":"respTwo"} 
------------------------------ 
MyResponseNewUnwrapped = {"meta":"metaNewUnwrapped","info":"infoNewUnwrapped","respNew":"respNewUnwrapped","respbBest":"respBestUnwrapped"}