猿问

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

我有一个包含多个 JSONObjects 的 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 是否有可能有这样的输出?


慕桂英546537
浏览 169回答 1
1回答

翻阅古今

抱歉,迟到的回复在过去 30 分钟内一直在敲我的键盘,但我终于完成了,这是代码。public static String getCSVData() throws IOException, JSONException {&nbsp; &nbsp; Path jsonFile = Paths.get("json");&nbsp; &nbsp; String json = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);&nbsp; &nbsp; JSONArray jsonArray = new JSONArray(json.trim());&nbsp; &nbsp; List<List<String>> jsonArrays = new ArrayList<>();&nbsp; &nbsp; for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> jsonObjects = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; JSONArray record = jsonArray.getJSONObject(i).getJSONArray("record");&nbsp; &nbsp; &nbsp; &nbsp; for (int i2 = 0; i2 < record.length(); i2++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = record.getJSONObject(i2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObjects.add(jsonObject.get("timeStamp").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObjects.add(jsonObject.get("code").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObjects.add(jsonObject.get("text").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObjects.add(jsonObject.get("hostname").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObjects.add(jsonObject.get("address").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObjects.add(jsonObject.get("type").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObjects.add(jsonObject.get("priority").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObjects.add(jsonObject.get("ttl").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; jsonArrays.add(jsonObjects);&nbsp; &nbsp; }&nbsp; &nbsp; StringBuilder stringBuilder = new StringBuilder("timeStamp,code,text,hostname,address,type,priority,ttl,hostname,address,type,priority,ttl\n");&nbsp; &nbsp; for(List<String> arrays : jsonArrays){&nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(StringUtils.join(arrays, ",")).append("\n");&nbsp; &nbsp; }&nbsp; &nbsp; return stringBuilder.toString().trim();}为了解释代码,我首先遍历第一个数组,然后获取第一个 JSONArray 的 jsonObject,然后从我通过遍历第一个 JSONArray 得到的 JsonObject 中获取名为“record”的 jsonArray,然后遍历 record 并获取所有项并将它们保存到 ArrayList 中。并通过 JDK 提供的 StringUtils 加入它们。如果你想把它写到文件中,请使用这个Files.write(Paths.get("YOUR CSV FILE"), getCSVData().getBytes(StandardCharsets.UTF_8));&nbsp;我使用的所有代码都是由 JDK 和 org.json 提供的。在我们打印出 getCSVDate(); 输出是:timeStamp,code,text,hostname,address,type,priority,ttl,hostname,address,type,priority,ttl2018-10-11T05:36:51+00:00,200,OK,qwe,192.168.1.1,A,0,3600,www,test.com,CNAME,0,36002018-10-11T05:36:52+00:00,200,OK,rty,192.168.1.2,A,0,300,*,test,CNAME,0,3600
随时随地看视频慕课网APP

相关分类

Java
我要回答