更简单的方式将JSON转换为CSV? [英] Easier way to convert JSON to CSV?

查看:163
本文介绍了更简单的方式将JSON转换为CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常流行的JSON字符串,如下所示:

I have a pretty wonky JSON string that looks like this:

[[{
    "Table":{
        "Properties":{
            "name":"ABOUT",
            "uniqueConstraints":"{}"
        },
        "Attributes": [{
            "name":"NAME",
            "unique":"true",
            "nullable":"false",
            "insertable":"false",
            "updatable":"false"
        },{
            "name":"VERSION",
            "unique":"true",
            "nullable":"false",
            "insertable":"false",
            "updatable":"false"   // I will need to go back through and add empty element for 'section'
        },{
            "name":"BUILD",           
            "insertable":"true",        
            "section": 1                
        }],
        "FileName": "About.java"
}]]

Entity | Property | Unique | Nullable | Insertable | Updatable | Section
=======+==========+========+==========+============+===========+=========
About  | Version  | True   | False    | False      | False     | 
About  | Build    | False  | False    | True       | False     | 1

我有大量的代码,我目前正在使用它来解析这个,只是想知道是否有人知道一个更简单的方法,我可以实现这一点,直接关联:

I have a ton of code that I'm currently using to parse this out, and I'm just wondering if anyone knows of an easier way I can accomplish this, with direct association with:


  1. 创建一个头数组,调用 header

  2. 创建一个csv数组,我们将调用 csv
  3. 循环浏览每个表格中的属性

  4. 一个新的数组 csv row

  5. < Table.Attr [i] .name
  6. 键/值在Attr中(除了名称Attr)

  7. 如果键不是 header csv [row] $ / code>

  8. 完成所有操作后,返回并向每一行添加额外的空元素。

  9. code>加入(,)+\\\

  1. Create a header array, we'll call it header
  2. Create a csv array, we'll call that csv
  3. Loop through each File and each table in file
  4. Loop through Attributes in each table
  5. Create a new array inside csv called row
  6. Push the table name & Table.Attr[i].name to row
  7. Parse through each key/value in the Attr (except the name Attr)
  8. If the key is something that isn't in header, push it
  9. Use header.indexOf(key) to decide where values should be placed in csv[row]
  10. After everything's done, go back through and add extra, empty elements to every row.
  11. For each row, Join(",") + "\n"


推荐答案

是的,不平凡的问题。我没有尝试过,但有一篇文章由一个章已经做了这些线。可能值得几分钟的时间,看看它是否接近你需要的 https://sunlightfoundation.com/blog/2014/03/11/making-json-as-simple-as-a-spreadsheet/

Yeah, non-trivial problem. I haven't tried it, but there is an article by a chap who has done something along these lines. Might be worth a few minutes of your time to see if it comes close to what you need https://sunlightfoundation.com/blog/2014/03/11/making-json-as-simple-as-a-spreadsheet/

他谈论处理数据数组和填充列...

He talks about dealing with arrays of data and padding out columns...

这篇关于更简单的方式将JSON转换为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆