在 Java 中将嵌套的任意 JSON 转换为 CSV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24778058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert nested arbitrary JSON to CSV in Java
提问by Ismail Sen
This question has been asked many times but I couldn't find the answer that fixes my issue.
这个问题已被问过很多次,但我找不到解决我问题的答案。
I'm trying to convert nested JSON format to CSV format like this :
我正在尝试将嵌套的 JSON 格式转换为 CSV 格式,如下所示:
The JSON structure is arbitraryand could be anything, nested or not.
该JSON结构是任意的,并可以是任何东西,嵌套与否。
I'm not suppose to know it, it's a database answer and I need to export this JSON answer into CSV file.
我不知道它,这是一个数据库答案,我需要将此 JSON 答案导出到 CSV 文件中。
Here is an example
这是一个例子
Input :
输入 :
{
"_id": 1,
"name": "Aurelia Menendez",
"scores": [
{
"type": "exam",
"score": 60.06045071030959
},
{
"type": "quiz",
"score": 52.79790691903873
},
{
"type": "homework",
"score": 71.76133439165544
}
]
}
The output I'm looking for :
我正在寻找的输出:
_id,name,scores.type,scores.score,scores.type,scores.score,scores.type,scores.score
1,Aurelia Menendez,exam,60.06...,quiz,52.79...,homework,71.76..
This is an example, it could be any other JSON document.
这是一个示例,它可以是任何其他 JSON 文档。
The idea here is to use dot notation in the CSV column name.
这里的想法是在 CSV 列名称中使用点表示法。
I've already used CDL but the output is not what I want :
我已经使用了 CDL,但输出不是我想要的:
_id scores name
1 "[{score:60.06045071030959,type:exam},{score:52.79790691903873,type:quiz},{score:71.76133439165544,type:homework}]" Aurelia Menendez
So how can I convert nested JSON to CSV with dot notation and in a generic way ?
那么如何使用点表示法和通用方式将嵌套的 JSON 转换为 CSV 呢?
Edits
编辑
Deserialisation of the JSON with Hymanson:
使用Hymanson对 JSON 进行反序列化:
ObjectMapper mapper=new ObjectMapper();
JsonNode jsonNode=mapper.readValue(new File("C:\...\...\...\test.json"), JsonNode.class);
Ismail
伊斯梅尔
回答by Ismail Sen
Comments are not convenient place to post longs answers, so I post my answer here.
评论不是发布长答案的方便地方,所以我在这里发布我的答案。
Analyze your JSON and all possible JSON structures you can get from your database. It should be a limited number of JSON forms.
As you have analyzed your JSON structure build a class/class hierarchy, that fully reflects this structure.
Use JSON serializer/deserializer libraryat your choice, to deserialize JSON to a java object.
Employ StringBuffer/StringBuilder classes, and iterate over your object information, and build comma delimited (or tab-delimited) strings.
Write strings you have built on the previous stage to the file.
分析您的 JSON 以及您可以从数据库中获得的所有可能的 JSON 结构。它应该是数量有限的 JSON 形式。
由于您已经分析了 JSON 结构,因此构建了一个类/类层次结构,它完全反映了这种结构。
使用您选择的JSON 序列化器/反序列化器库,将 JSON 反序列化为 java 对象。
使用 StringBuffer/StringBuilder 类,并迭代您的对象信息,并构建逗号分隔(或制表符分隔)字符串。
将您在前一阶段构建的字符串写入文件。
That's it.
就是这样。
回答by ahaaman
Converting JSON to XLS/CSV in Javahas what you are looking for.
在 Java 中将 JSON 转换为 XLS/CSV具有您正在寻找的内容。
Basically, you need to use org.json.CDL to convert from JSON to CSV format
基本上,您需要使用 org.json.CDL 将 JSON 格式转换为 CSV 格式
回答by skap
Like you said :
就如你所说的 :
The JSON structure is arbitraryand could be anything, nested or not.
该JSON结构是任意的,并可以是任何东西,嵌套与否。
The JSON to CSV conversion can't be generalized as it varies from user to user and also depends specific requirements.
JSON 到 CSV 的转换不能一概而论,因为它因用户而异,并且还取决于特定要求。
But still there's a libraryjson2flatwhich tries to achieve it. But it may differ from user's requirement. Still it's worth a try.
但是仍然有一个库json2flat试图实现它。但它可能与用户的要求不同。还是值得一试的。
For example for the JSON given above:
例如对于上面给出的 JSON:
{
"_id": 1,
"name": "Aurelia Menendez",
"scores": [
{
"type": "exam",
"score": 60.06045071030959
},
{
"type": "quiz",
"score": 52.79790691903873
},
{
"type": "homework",
"score": 71.76133439165544
}
]
}
can be interpreted as follows :
可以解释如下:
/_id,/name,/scores/type,/scores/score
1,"Aurelia Menendez","exam",60.06045071030959
1,"Aurelia Menendez","quiz",52.79790691903873
1,"Aurelia Menendez","homework",71.76133439165544