如何在JSON体中扩展和对象?

时间:2016-08-26 10:26:01

标签: java json jackson

我有下一个类来拥有适当的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;

    //...
}

我需要的JSON:

{
  "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?或者还有其他方法可以做这样的事吗?主要关注的是不重复每个Object的代码。

1 个答案:

答案 0 :(得分:3)

您可以使用公共类继承或将其添加为未包装的对象:

选项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"}