将 JSON 格式转换为 MS Excel 的 CSV 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4130849/
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 JSON format to CSV format for MS Excel
提问by stockoverflow
I received a JSON file but don't know how to read it. Is there a converter where I can produce a nice CSV file so it can be loaded into MS Excel? I don't understand JSON, so it would be awesome if someone wrote a script or link me to one that would do the job.
我收到了一个 JSON 文件,但不知道如何阅读它。是否有转换器可以生成一个不错的 CSV 文件,以便将其加载到 MS Excel 中?我不懂 JSON,所以如果有人写了一个脚本或将我链接到一个可以完成这项工作的脚本,那就太棒了。
I found something close at http://json.bloople.netbut, unfortunately, it's JSON to HTML.
我在http://json.bloople.net 上找到了一些接近的东西,但不幸的是,它是 JSON 到 HTML。
Edit: jsonformat.com gets even closer, however it's still not CSV.
编辑:jsonformat.com 更接近,但它仍然不是 CSV。
采纳答案by Zachary
I'm not sure what you're doing, but this will go from JSON to CSV using JavaScript. This is using the open source JSON library, so just download JSON.js into the same folder you saved the code below into, and it will parse the static JSON value in json3into CSV and prompt you to download/open in Excel.
我不确定您在做什么,但这将使用 JavaScript 从 JSON 转换为 CSV。这是使用开源 JSON 库,因此只需将 JSON.js 下载到您保存以下代码的同一文件夹中,它就会将静态 JSON 值解析json3为 CSV 并提示您在 Excel 中下载/打开。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JSON to CSV</title>
<script src="scripts/json.js" type="text/javascript"></script>
<script type="text/javascript">
var json3 = { "d": "[{\"Id\":1,\"UserName\":\"Sam Smith\"},{\"Id\":2,\"UserName\":\"Fred Frankly\"},{\"Id\":1,\"UserName\":\"Zachary Zupers\"}]" }
DownloadJSON2CSV(json3.d);
function DownloadJSON2CSV(objArray)
{
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
line += array[i][index] + ',';
}
// Here is an example where you would wrap the values in double quotes
// for (var index in array[i]) {
// line += '"' + array[i][index] + '",';
// }
line.slice(0,line.Length-1);
str += line + '\r\n';
}
window.open( "data:text/csv;charset=utf-8," + escape(str))
}
</script>
</head>
<body>
<h1>This page does nothing....</h1>
</body>
</html>
回答by Joseph Sturtevant
I created a JsFiddle herebased on the answer given by Zachary. It provides a more accessible user interface and also escapes double quotes within strings properly.
我根据Zachary 给出的答案在这里创建了一个 JsFiddle 。它提供了一个更易于访问的用户界面,并正确地转义字符串中的双引号。
回答by Palesz
You can use that gist, pretty easy to use, stores your settings in local storage: https://gist.github.com/4533361
您可以使用该要点,非常易于使用,将您的设置存储在本地存储中:https: //gist.github.com/4533361
回答by chinuy
Using Python will be one easy way to achieve what you want.
使用 Python 将是实现您想要的一种简单方法。
I found one using Google.
我用谷歌找到了一个。
"convert from json to csv using python" is an example.
“使用python从json转换为csv”就是一个例子。

