-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
114 lines (96 loc) · 2.25 KB
/
index.html
File metadata and controls
114 lines (96 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<html>
<head>
<title>Covnert JSON to CSV</title>
<meta charset="utf8">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src=" http://libs.baidu.com/json/json2/json2.js"></script>
<style>
button {
width: 50px;
height: 20px;
margin-left: 1%;
}
#txtJSON {
height: 40%;
width: 98%;
margin: 1% 1% 1% 1%;
border-color: #ccc;
background-color:#f8f8f8;
line-height:150%
}
.display{
margin: 1% 1% 1% 1%;
}
#csv {
border: 1px #ccc solid;
background-color:#f8f8f8;
line-height:150%
}
pre{
height: 40%;
white-space:pre-wrap;
white-space:-moz-pre-wrap;
white-space:-pre-wrap;
white-space:-o-pre-wrap;
word-wrap:break-word;
overflow:auto;
}
</style>
</head>
<body>
<!------输入框------->
<div>
<textarea class="form-control" id="txtJSON" placeholder="粘贴Json文本到这里"></textarea>
</div>
<!------按钮1------->
<div>
<button id="convert" type="button" class="btn btn-sm" > 转换 </button>
</div>
<!------结果框------->
<div class="display">
<!--<h3>JSON</h3>
<pre id="json"></pre>
<h3>CSV</h3>-->
<pre id="csv"></pre>
</div>
<!------按钮2------->
<div>
<a id="download" download="json.csv" href="#" onclick="downloader(this)">
<button type="button" class="btn btn-sm" > 下载 </button>
</a>
</div>
</body>
<script type="text/javascript">
// JSON to CSV Converter
function ConvertToCSV(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]) {
if (line != '') line += ','
line += '"'+array[i][index]+'"';
}
str += line + '\r\n';
}
return str;
}
//生成下载文件
function downloader(aLink){
var str = $('#csv').text();;
str = encodeURIComponent(str);
aLink.href = "data:text/csv;charset=utf-8,\ufeff"+str;
};
//按钮点击
$("button#convert").click(function(){
// Create Object
var items = $('#txtJSON').val();
// Convert Object to JSON
//var jsonObject = JSON.stringify(items);
// Display JSON
//$('#json').text(jsonObject);
// Convert JSON to CSV & Display CSV
$('#csv').text(ConvertToCSV(items));
});
</script>
</html>