更简单的方式将JSON转换为CSV? [英] Easier way to convert JSON to 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:
- 创建一个头数组,调用
header
- 创建一个csv数组,我们将调用
csv
- 循环浏览每个表格中的属性
- 一个新的数组
csv
row
- 键/值在Attr中(除了名称Attr)
- 如果键不是
header
csv [row] $/ code>
- 完成所有操作后,返回并向每一行添加额外的空元素。
- code>加入(,)+\\\
<
Table.Attr [i] .name
到行
- Create a header array, we'll call it
header
- Create a csv array, we'll call that
csv
- Loop through each File and each table in file
- Loop through Attributes in each table
- Create a new array inside
csv
calledrow
- Push the table name &
Table.Attr[i].name
torow
- Parse through each key/value in the Attr (except the name Attr)
- If the key is something that isn't in
header
, push it - Use
header.indexOf(key)
to decide where values should be placed incsv[row]
- After everything's done, go back through and add extra, empty elements to every row.
- 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屋!