在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:48:40  来源:igfitidea点击:

Convert nested arbitrary JSON to CSV in Java

javajsoncsvconverter

提问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.

评论不是发布长答案的方便地方,所以我在这里发布我的答案。

  1. Analyze your JSON and all possible JSON structures you can get from your database. It should be a limited number of JSON forms.

  2. As you have analyzed your JSON structure build a class/class hierarchy, that fully reflects this structure.

  3. Use JSON serializer/deserializer libraryat your choice, to deserialize JSON to a java object.

  4. Employ StringBuffer/StringBuilder classes, and iterate over your object information, and build comma delimited (or tab-delimited) strings.

  5. Write strings you have built on the previous stage to the file.

  1. 分析您的 JSON 以及您可以从数据库中获得的所有可能的 JSON 结构。它应该是数量有限的 JSON 形式。

  2. 由于您已经分析了 JSON 结构,因此构建了一个类/类层次结构,它完全反映了这种结构。

  3. 使用您选择的JSON 序列化器/反序列化器库,将 JSON 反序列化为 java 对象。

  4. 使用 StringBuffer/StringBuilder 类,并迭代您的对象信息,并构建逗号分隔(或制表符分隔)字符串。

  5. 将您在前一阶段构建的字符串写入文件。

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