使用json-simple libary从Flexigrid生成JSON [英] Generate JSON from Java for Flexigrid using json-simple libary

查看:173
本文介绍了使用json-simple libary从Flexigrid生成JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下预期的JSON格式,我试图使用Java生成JSON:

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



我到目前为止写了下面的代码,它不包含支持嵌套对象的逻辑。我发现这很难,因为表示一个JSON对象所需的代码使用的是一个HashMap类型的对象,它需要唯一的键。所以在我需要有多个id条目,我只能有一个输入与id的哈希键。我也尝试了谷歌/ Guava multimap,但有一个限制,试图投射或混合JSON库和谷歌实现在同一时间。

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


推荐答案

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

这篇关于使用json-simple libary从Flexigrid生成JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆