JACKSON,JSON:列出标题

时间:2016-03-31 09:42:02

标签: java json spring jackson

我正在使用Jackson lib来生成Json个文件。

创建bean类后

Class ActiveOrderResponse

public class ActiveOrderResponse implements IWsResponse {

    @JsonProperty("error")
    public String errorMsg;

    @JsonProperty("errorDsc")
    public String errorDesc = "";

    @JsonProperty("Orders")
    public ArrayList<ActiveOrder> orders = new ArrayList<ActiveOrder>();
}

Class ActiveOrder

public class ActiveOrder {

    @JsonProperty("OrderNo")
    private String orderNo;

    @JsonProperty("Status")
    private String status;

    @JsonProperty("Description")
    private String description = "";
}

我在结果中有Json

{"error":"000",
"errorDsc":"",
"Orders":[
{"OrderNo":"Order_1",
"Status":"NOT_EXISTS",
"Description":""},
{"OrderNo":"Order_2",
"Status":"COMPLETED",
"Description":""},
{"OrderNo":"Order_3",
"Status":"CREATED",
"Description":""},
{"OrderNo":"Order_4",
"Status":"NOT_IMPORTED",
"Description":""}]
}

但是我让Json像这样(每个订单列表开头的标题“Order”):

{"error":"000",
"errorDsc":"",
"Orders":[
"Order" : {"OrderNo":"Order_1",
"Status":"NOT_EXISTS",
"Description":""},
"Order" : {"OrderNo":"Order_2",
"Status":"COMPLETED",
"Description":""},
"Order" : {"OrderNo":"Order_3",
"Status":"CREATED",
"Description":""},
"Order" : {"OrderNo":"Order_4",
"Status":"NOT_IMPORTED",
"Description":""}]
}

1 个答案:

答案 0 :(得分:0)

我的环境,如果问题没有解决,请告诉我使用ObjectMapper的代码:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.6</version>
</dependency>


public static void main(String[] args) throws IOException {
    ActiveOrderResponse activeOrderResponse = new ActiveOrderResponse();
    activeOrderResponse.orders = new ArrayList<ActiveOrder>();
    activeOrderResponse.orders.add(new ActiveOrder());
    ObjectMapper om = new ObjectMapper();
    om.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
    om.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, true);
    String json = om.writeValueAsString(activeOrderResponse);
    System.out.println(json);
}


{
    "error" : null,
        "errorDsc" : "",
        "Orders" : [ {
    "OrderNo" : null,
            "Status" : null,
            "Description" : ""
} ]
}