繁体   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