将MS Excel的JSON格式转换为CSV格式 [英] Convert JSON format to CSV format for MS Excel

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

问题描述

我收到了JSON文件,但不知道如何读取.有转换器可以在其中生成漂亮的CSV文件,以便将其加载到MS Excel中吗?我不懂JSON,所以如果有人写了一个脚本或将我链接到可以完成此工作的脚本,那真是太棒了.

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.

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

I found something close at http://json.bloople.net but, unfortunately, it's JSON to HTML.

jsonformat.com距离更近,但是仍然不是CSV.

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

推荐答案

我不确定您在做什么,但这将使用JavaScript从JSON转换为CSV.这是使用开源JSON库,因此只需将JSON.js下载到您保存的同一文件夹中下面的代码插入其中,它将把json3中的静态JSON值解析为CSV,并提示您下载/在Excel中打开.

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.

<!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>

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

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