Generate JSON from Java for Flexigrid using json-simple libary

 ̄綄美尐妖づ 提交于 2019-12-12 04:54:51

问题


Given the following expected JSON format, I am trying to generate JSON using Java:

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

I have thus far written the following code which is close in some regard, but it does not contain logic to supported nested objects. I find this difficult, because the code required to represent a JSON object uses a object that is of a HashMap type which requires unique keys. So where I need to have multiple "id" entries, I can only have one hashmap key entered with id. I also tried with google / Guava multimap, but there is a limitation in trying to cast or mix the JSON library and google implementation at the same time.

The below code generates this as the output:

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

which you can see from above, would be close but incorrect because it will only contain 1 x row and cell.

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

回答1:


Trying below code this might work.

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

Output:

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



回答2:


you can do it as a two step process if thats not a problem.

1) generate the xml equivalent of json you want in java using the Document builder.

2)converting xml into its json equivalent is quite easy then... hope this helps!



来源:https://stackoverflow.com/questions/25366924/generate-json-from-java-for-flexigrid-using-json-simple-libary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!