生成JSON数组

时间:2014-02-26 13:24:46

标签: java json arraylist

我需要使用Java生成如下所示的JSON数组对象(“MainData”)。任何人都可以向我建议如何做到这一点?

{
  "MainData":{
    "columnHeaderKeys":null,
    "rowHeaders":[
      {
        "id":0001,
        "name":abcd
      },
      {
        "id":0002,
        "name":xyz
      }
    ],
    "data":[
      {
        "id":0001,
        "rowId":"R1",
        "status":PASSED
      },
      {
        "id":0002,
        "rowId":"R2",
        "status":PASSED
      }
    ]
  }
}

4 个答案:

答案 0 :(得分:3)

使用Google GSON,非常简单:

JSONObject result = new JSONObject();
result.put("columnHeadersKeys", JSONObject.NULL);

JSONArray headers = new JSONArray();

JSONObject obj = new JSONObject();
obj.put("id", 1);
obj.put("name", "abcd");
headers.put(obj);

obj = new JSONObject();
obj.put("id", 2);
obj.put("name", "xyz");
headers.put(obj);

result.put("rowHeaders", headers);

JSONArray data = new JSONArray();

obj = new JSONObject();
obj.put("id", 1);
obj.put("rowId", "R1");
obj.put("status", "PASSED");
data.put(obj);

obj = new JSONObject();
obj.put("id", 2);
obj.put("rowId", "R2");
obj.put("status", "PASSED");
data.put(obj);

result.put("data", data);

String output = result.toString();

请注意,对象的整个创建可以在一个语句中链接 - 但是当它被拆分时我发现它更容易阅读。

答案 1 :(得分:2)

使用Jackson库。

来自the tutorial的示例代码:

ObjectMapper mapper = new ObjectMapper();

Map<String,Object> userData = new HashMap<String,Object>();
Map<String,String> nameStruct = new HashMap<String,String>();
nameStruct.put("first", "Joe");
nameStruct.put("last", "Sixpack");
userData.put("name", nameStruct);
userData.put("gender", "MALE");
userData.put("verified", Boolean.FALSE);
userData.put("userImage", "Rm9vYmFyIQ==");

mapper.writeValue(new File("user-modified.json"), userData);

答案 2 :(得分:0)

看看here。你会发现许多项目可以帮助你。

我会从Crockford的JSONObject.java Github存储库中尝试JSON for Java

简单用法:

JSONObject obj = new JSONObject()
    .put("MainData", new JSONObject()
        .put("columnHeaderKeys", null)
        .put("rowHeaders", new JSONArray()
            .put(new JSONObject()
                .put("id", "001")
                .put("name", "abc")
            )
            .put(new JSONObject()
                .put("id", "002")
                .put("rowId", "R1")
            // ...
        )
    );

输出字符串:

String jsonString = obj.toString();

或直接写入文件:

obj.write(new FileWriter(new File("/path/to/destination/file")));

答案 3 :(得分:0)

你可以这样做这对我来说非常适合这条线 Log.i("MainJSON", MainJSON.toString());我已打印创建的JSONObject

try{
        JSONObject jo1=new JSONObject();
        jo1.put("id",001);
        jo1.put("name","abcd");

        JSONObject jo2=new JSONObject();
        jo2.put("id",002);
        jo2.put("name","xyz");

        JSONArray jarr1=new JSONArray();
        jarr1.put(0, jo1);
        jarr1.put(1, jo2);

        JSONObject jo3=new JSONObject();
        jo3.put("id",0001);
        jo3.put("rowId","R1");
        jo3.put("status","PASSED");

        JSONObject jo4=new JSONObject();
        jo4.put("id",0002);
        jo4.put("rowId","R2");
        jo4.put("status","PASSED");

        JSONArray jarr2=new JSONArray();
        jarr2.put(0, jo3);
        jarr2.put(1, jo4);

        JSONObject MainDataObj=new JSONObject();
        MainDataObj.put("rowHeaders", jarr1);
        MainDataObj.put("data", jarr2);
        MainDataObj.put("columnHeaderKeys", "null");

        JSONObject MainJSON=new JSONObject();
        MainJSON.put("MainData", MainDataObj);

        Log.i("MainJSON", MainJSON.toString());
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }