在java中从列表转换为json

时间:2016-05-03 07:42:59

标签: java json

我有一个列表,我想将其转换为json。

这是我的代码

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);

我希望将其转换为以下格式:

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

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

首先: 使用 泛型

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

其次:你能用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尚未对它进行测试,但它应该可行,我在我的项目中这样做,没关系,如果你有更多麻烦请告诉我!

答案 1 :(得分:1)

如果您想手动执行此操作,您可以:

创建一个从tour对象生成JSON的方法:

class JqGridModel{

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

然后在列表内容上调用此方法:

JSONObject json = new JSONObject();
JSONArray arr = new JSONArray();
for(JqGridModel m: jqGridModels){
     arr.put(m.toJSON());
}
json.accumulate("rows", arr);

答案 2 :(得分:0)

    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();
    }