将JSON格式转换为MS Excel的CSV格式

时间:2023-01-15 09:17:59

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.net but, unfortunately, it's JSON to HTML.

我在http://json.bloople.net找到了一些东西,但不幸的是,它是JSON。

Edit: jsonformat.com gets even closer, however it's still not CSV.

编辑:jsonformat.com更加接近了,但是它仍然不是CSV。

4 个解决方案

#1


64  

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 json3 into CSV and prompt you to download/open in Excel.

我不确定你在做什么,但这将从JSON到CSV使用JavaScript。这使用的是开源JSON库,因此只需下载JSON。将js保存到您将下面的代码保存到的同一文件夹中,它将把json3中的静态JSON值解析为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>

#2


62  

I created a JsFiddle here based on the answer given by Zachary. It provides a more accessible user interface and also escapes double quotes within strings properly.

我创建了一个JsFiddle基于Zachary给出的答案。它提供了一个更容易访问的用户界面,并且正确地在字符串中转义双引号。

#3


2  

Using Python will be one easy way to achieve what you want.

使用Python将是实现您想要的东西的一种简单方法。

I encourage you to Google first, because I found one using Google.

我鼓励大家先来看看谷歌,因为我用谷歌找到了一个。

"convert from json to csv using python" is an example.

“使用python将json转换为csv”就是一个例子。

#4


1  

You can use that gist, pretty easy to use, stores your settings in local storage: https://gist.github.com/4533361

您可以使用这个gist,非常容易使用,它将您的设置存储在本地存储中:https://gist.github.com/4533361

#1


64  

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 json3 into CSV and prompt you to download/open in Excel.

我不确定你在做什么,但这将从JSON到CSV使用JavaScript。这使用的是开源JSON库,因此只需下载JSON。将js保存到您将下面的代码保存到的同一文件夹中,它将把json3中的静态JSON值解析为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>

#2


62  

I created a JsFiddle here based on the answer given by Zachary. It provides a more accessible user interface and also escapes double quotes within strings properly.

我创建了一个JsFiddle基于Zachary给出的答案。它提供了一个更容易访问的用户界面,并且正确地在字符串中转义双引号。

#3


2  

Using Python will be one easy way to achieve what you want.

使用Python将是实现您想要的东西的一种简单方法。

I encourage you to Google first, because I found one using Google.

我鼓励大家先来看看谷歌,因为我用谷歌找到了一个。

"convert from json to csv using python" is an example.

“使用python将json转换为csv”就是一个例子。

#4


1  

You can use that gist, pretty easy to use, stores your settings in local storage: https://gist.github.com/4533361

您可以使用这个gist,非常容易使用,它将您的设置存储在本地存储中:https://gist.github.com/4533361