簡體   English   中英

使用json-簡單庫從Java為Flexigrid生成JSON

[英]Generate JSON from Java for Flexigrid using json-simple libary

給定以下預期的JSON格式,我嘗試使用Java生成JSON:

{
  "stat": "ok",
  "page": 1,
  "total": 100,
  "rows": [
     {
       "id":"1",
       "cell":[
         "content of column 1",
         "content of column 2",
         "content of column 3",
         "content of column 4", 
         "content of column 5",
         "content of column 6",
         "content of column 7",
         "content of column 8",
         "content of column 9"
       ]
    },  
     {
       "id":"2",
       "cell":[
         "content of column 1",
         "content of column 2",
         "content of column 3",
         "content of column 4", 
         "content of column 5",
         "content of column 6",
         "content of column 7",
         "content of column 8",
         "content of column 9"
       ]
    },

到目前為止,我已經編寫了以下在某些方面很接近的代碼,但其中不包含支持嵌套對象的邏輯。 我發現這很困難,因為表示JSON對象所需的代碼使用的是HashMap類型的對象,該對象需要唯一的鍵。 因此,在需要多個“ id”條目的地方,我只能輸入一個帶有id的hashmap鍵。 我也嘗試使用google / Guava multimap,但嘗試同時投射或混合JSON庫和google實現存在局限性。

下面的代碼將其生成為輸出:

{
  "total": "100",
  "page": "1",
  "stat": "ok",
  "rows": [
    {
      "id": "1",
      "cell": [
        "test which represents one column's content",
        "test, which contains 2nd column's data"
      ]
    }
  ]
}

您可以從上方看到它,但它很接近但不正確,因為它僅包含1 x行和單元格。

public class GenerateJSON {

    public static void main(String[] args) {

        JSONArray cell = new JSONArray();
        cell.add("test which represents one column's content");
        cell.add("test, which contains 2nd column's data");

        JSONObject ids = new JSONObject();
        ids.put("id", "1");
        ids.put("cell",cell);

        HashMap<String,String> map = new HashMap(ids);

        JSONArray rows = new JSONArray();
        rows.add(ids);

        JSONObject obj = new JSONObject();
        obj.put("stat","ok");
        obj.put("total","100");
        obj.put("page","1");
        obj.put("rows",rows);

        System.out.println(obj);    
    }
}

嘗試下面的代碼,這可能會起作用。

public static void main(String[] args) throws JSONException {

    JSONObject obj = null;

    JSONArray cellArray = new JSONArray();
    JSONArray cellArray1 = new JSONArray();

    for (int i = 0; i < 2; i++) {
        obj = new JSONObject();

        obj.put("id", i);
        obj.put("cell", "contecnt 1, content 2, content 3");

        cellArray.put(obj);
    }

    JSONObject responseData = new org.json.JSONObject();

    responseData.put("rows", cellArray);

//      System.out.println(responseData);

    JSONObject obj1 = new JSONObject();

    obj1.put("stat", "ok");
    obj1.put("total", "100");
    obj1.put("page", 1);
    obj1.put("rows", responseData);

    cellArray1.put(obj1);

    JSONObject response = new org.json.JSONObject();

    response.put("data", cellArray1);

    System.out.println(response);
}

輸出:

{"data":[{"total":"100","page":1,"stat":"ok",
"rows":
{"rows":[
  {"id":0,
  "cell":"contecnt 1, content 2, content 3"},

  {"id":1,
  "cell":"contecnt 1, content 2, content 3"}
 ]}
}]}

如果那不是問題,則可以將其分為兩個步驟進行。

1)使用文檔構建器在Java中生成所需的json的xml等效文件。

2)然后將xml轉換成它的json相當容易...希望這會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM