简体   繁体   中英

convert from list to json in java

I have a list and I want to convert it to json.

Here is my code

JqGridModel gridModel1 = new JqGridModel();
Date FromDate = new Date("1996-07-04");
Date ToDate = new Date("1996-07-05");
gridModel1.setOrderID(10248);
gridModel1.setFromDate(FromDate);
gridModel1.setToDate(ToDate);
gridModel1.setCustomerID("WILMK");
gridModel1.setShipName("Vins et alcools Chevalier");

JqGridModel gridModel2 = new JqGridModel();
Date FromDate2 = new Date("1996-07-04");
Date ToDate2 = new Date("1996-07-05");
gridModel2.setOrderID(10248);
gridModel2.setFromDate(FromDate2);
gridModel2.setToDate(ToDate2);
gridModel2.setCustomerID("WILMK");
gridModel2.setShipName("Vins et alcools Chevalier");



List jqGridModels = new ArrayList();
jqGridModels.add(gridModel1);
jqGridModels.add(gridModel2);

And I want to convert it to be in the following format:

{ 
    "rows":[
        {"OrderID":"10248","FromDate":"1996-07-04","CustomerID":"WILMK","ShipName":"Vins et alcools Chevalier","ToDate":"1996-07-05"},
        {"OrderID":"10249","FromDate":"1996-07-05","CustomerID":"TRADH","ShipName":"Toms Spezialit\u00e4ten","ToDate":"1996-07-17"},
        {"OrderID":"10250","FromDate":"1996-07-08","CustomerID":"HANAR","ShipName":"Hanari Carnes","ToDate":"1996-07-26"},
        {"OrderID":"10251","FromDate":"1996-07-08","CustomerID":"VICTE","ShipName":"Victuailles en stock","ToDate":"1996-08-01"},
        {"OrderID":"10252","FromDate":"1996-07-09","CustomerID":"SUPRD","ShipName":"Supr\u00eames d\u00e9lices","ToDate":"1996-08-01"}
    ]
}

Any help would be greatly appreciated.

Firstly: use generics

List jqGridModels = new ArrayList();  //bad
List<JqGridModel> jqGridModels = new ArrayList<>();  //good

Secondly: Can you use Gson?

List<JqGridModel> jqGridModels = new ArrayList<>();

Gson gson = new Gson();
JsonElement jsElem = gson.toJsonTree(jqGridModels, new TypeToken<List<JqGridModel>>() {}.getType());

if (! jsElem.isJsonArray()) {
    //this is an error...
}

JsonArray jsonArray = jsElem.getAsJsonArray();

Haven't tested it yet but it should work, I'm doing this in my project and it's ok, if you have more troubles please tell me!

If you want to do it manually you can :

Create a method to generate the JSON from tour object :

class JqGridModel{

    public JSONObject toJSON(){
        JSONObject json = new JSONObject();
        json.accumulate("OrderID", orderID);
        // DO the same for all attributes
        return  json;
    }
}

And then call this method looping on the list content :

JSONObject json = new JSONObject();
JSONArray arr = new JSONArray();
for(JqGridModel m: jqGridModels){
     arr.put(m.toJSON());
}
json.accumulate("rows", arr);
    com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
    StringBuffer result = new StringBuffer("{\n\t\"rows\":[\n");
    try {
        for(Object item : jqGridModels){
            result = result.append("\t\t\t");
            result = result.append(mapper.writeValueAsString(item));
            result = result.append("\n");
        }
        if(jqGridModels.size() > 1){
            result = result.append("\t]\n}");
            System.out.print(result);
        }
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM