java - 如何将嵌套的 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,是否有可能得到这样的输出?
解决方案
抱歉,在过去的 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,然后从我通过循环第一个 JSONArray 获得的 JsonObject 中获取名为“record”的 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
推荐阅读
- python - 将 mongoDB 链接到 python 代码?
- javascript - react navigator react-native-sectioned-multi-select 如何转换成数组项
- python - 使用 Python3 和 SQLlite 存储字典数据
- c# - 显示对象名称,并添加到列表
- json - 在我的 Django 后端接收来自 ReactJS 的 JSON 对象
- firebase - Firebase 基于角色的授权
- python - 如何在 SqlAlchemy 中使用 pandas 进行 upsert
- python - 如何从 pandas DataFrame 列中提取美元金额
- excel - 在 64 位 Excel 中从 VBA 编写注册表
- c# - C# - 如何绘制具有 3 个变量的图表(Position[32]、ByteValue[255]、Frequency[int])