0

I'm using Jackson lib to generate Jsonfiles.

after creating the bean class,

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 = "";
}

i have this Json in the Result :

{"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":""}]
}

But i went the Json be like this (a title "Order" in the beginning of every Order List):

{"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":""}]
}
zebiri djallil
  • 324
  • 2
  • 6
  • 17

1 Answers1

0

My environment, if problem not solved , show me the code with 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" : ""
} ]
}
贾宇鹏
  • 11
  • 3