有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将嵌套JSONArray导出到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) 个答案

  1. # 1 楼答案

    很抱歉,在过去的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();
    }
    

    为了首先解释代码,我在第一个数组上循环,然后获取第一个JSONArray的jsonObject,然后从jsonObject中获取名为“record”的JSONArray,我通过在第一个JSONArray上循环,然后在record上循环,获取所有项,并将它们保存到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