如何将嵌套的JSONArray导出到CSV?

时间:2018-10-11 05:58:36

标签: java arrays json api csv

我有一个包含多个JSONObject的JSONArray

[
   {
      "record":[
         {
            "timeStamp":"2018-10-11T05:36:51+00:00",
            "code":200,
            "text":"OK"
         },
         {
            "hostname":"qwe",
            "address":"192.168.1.1",
            "type":"A",
            "priority":"0",
            "ttl":"3600"
         },
         {
            "hostname":"www",
            "address":"test.com",
            "type":"CNAME",
            "priority":"0",
            "ttl":"3600"
         }
      ]
   },
   {
      "record":[
         {
            "timeStamp":"2018-10-11T05:36:52+00:00",
            "code":200,
            "text":"OK"
         },
         {
            "hostname":"rty",
            "address":"192.168.1.2",
            "type":"A",
            "priority":"0",
            "ttl":"300"
         },
         {
            "hostname":"*",
            "address":"test",
            "type":"CNAME",
            "priority":"0",
            "ttl":"3600"
         }
      ]
   }
]

如何解析此JSONArray并将其导出为CSV文件。

这是我到目前为止尝试过的

    File file=new File("/home/administrator/Desktop/test.csv");
    String csv = jsonArray;
    FileUtils.writeStringToFile(file, csv);
    System.out.println("CSV created.");

我想要的输出是

timeStamp,code,text,hostname,address,type,priority,ttl,hostname,address,type,priority,ttl
    2018-10-11T05:36:51+00:00,200,OK,qwe,192.168.1.1,A,0,300,www,test.com,CNAME,0,3600
    2018-10-11T05:36:52+00:00,200,OK,rty,192.168.1.2,A,0,300,*,test,CNAME,0,3600

鉴于上面的JSONArray,是否可能有这样的输出?

1 个答案:

答案 0 :(得分:0)

抱歉,过去30分钟来我一直在敲击键盘,但我终于完成了,这里是代码。

public static String getCSVData() throws IOException, JSONException {
    Path jsonFile = Paths.get("json");
    String json = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
    JSONArray jsonArray = new JSONArray(json.trim());
    List<List<String>> jsonArrays = new ArrayList<>();
    for (int i = 0; i < jsonArray.length(); i++) {
        List<String> jsonObjects = new ArrayList<>();
        JSONArray record = jsonArray.getJSONObject(i).getJSONArray("record");
        for (int i2 = 0; i2 < record.length(); i2++) {
            JSONObject jsonObject = record.getJSONObject(i2);
            if (i2 == 0) {
                jsonObjects.add(jsonObject.get("timeStamp").toString());
                jsonObjects.add(jsonObject.get("code").toString());
                jsonObjects.add(jsonObject.get("text").toString());
            } else {
                jsonObjects.add(jsonObject.get("hostname").toString());
                jsonObjects.add(jsonObject.get("address").toString());
                jsonObjects.add(jsonObject.get("type").toString());
                jsonObjects.add(jsonObject.get("priority").toString());
                jsonObjects.add(jsonObject.get("ttl").toString());
            }
        }
        jsonArrays.add(jsonObjects);
    }
    StringBuilder stringBuilder = new StringBuilder("timeStamp,code,text,hostname,address,type,priority,ttl,hostname,address,type,priority,ttl\n");
    for(List<String> arrays : jsonArrays){
        stringBuilder.append(StringUtils.join(arrays, ",")).append("\n");
    }
    return stringBuilder.toString().trim();
}

为解释代码,我首先遍历第一个Array,然后获取第一个JSONArray的jsonObject,然后从我通过遍历第一个JSONArray然后在遍历记录和获取所有项目并将其保存到ArrayList中。并通过JDK提供的StringUtils将它们加入。

如果要将其写入文件,请使用此

Files.write(Paths.get("YOUR CSV FILE"), getCSVData().getBytes(StandardCharsets.UTF_8)); 

我使用的所有代码均由JDK和org.json提供。

在我们打印出getCSVDate()之后;输出为:

timeStamp,code,text,hostname,address,type,priority,ttl,hostname,address,type,priority,ttl
2018-10-11T05:36:51+00:00,200,OK,qwe,192.168.1.1,A,0,3600,www,test.com,CNAME,0,3600
2018-10-11T05:36:52+00:00,200,OK,rty,192.168.1.2,A,0,300,*,test,CNAME,0,3600