-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-parser.html
185 lines (170 loc) · 6.95 KB
/
json-parser.html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bower/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="bower/font-awesome/css/font-awesome.min.css" />
<title>JSON Parser</title>
</head>
<body>
<div class="container">
<h2>
<i class="fa fa-industry" aria-hidden="true"></i> JSON Parser
<a href="index.html" class="btn btn-default">
<i class="fa fa-fa-reply" aria-hidden="true"></i> Back to Menu
</a>
</h2>
<div class="row">
<div class="col-xs-12 text-info" id="dropArea" style="outline: 1px solid orange;height:50px;padding-top:15px;">
<i class="fa fa-cloud-upload" aria-hidden="true"></i>
Drag & Drop text file to this Area. (Only [*.txt])
</div>
</div>
<div class="row">
<div class="col-xs-12 text-warning" id="dropResult">
</div>
</div>
<br/>
<div class="row">
<div class="col-xs-6" style="margin-bottom:10px;">
Input:
<label>
<input type="radio" name="jsonType" id="jsonTypeJson" value="json" checked> JSON
</label>
<label>
<input type="radio" name="jsonType" id="jsonTypeJson5" value="json5"> JSON5
</label>
<button id="reset" class="btn btn-warning">
<i class="fa fa-eraser" aria-hidden="true"></i> Reset
</button>
</div>
<div class="col-xs-6" style="margin-bottom:10px;">
Output:
<label>
<input type="radio" name="outType" id="outTypeJson" value="json" checked> JSON
</label>
<label>
<input type="radio" name="outType" id="outTypeJson5" value="json5"> JSON5
</label>
<label>
<input type="radio" name="format" id="format" value="format" checked> Format
</label>
<label>
<input type="radio" name="format" id="unformat" value="unformat"> Un Format
</label>
</div>
</div>
<div class="row">
<div class="col-xs-6">
<textarea class="form-control" rows="13" id="json" placeholder="Input Text(JSON)"></textarea>
</div>
<div class="col-xs-6">
<textarea class="form-control" rows="13" id="parsed" readonly placeholder="Formatted JSON"></textarea>
</div>
</div>
</div>
<script src="bower/jquery/dist/jquery.min.js"></script>
<script src="bower/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower/json5/lib/json5.js"></script>
<script>
var parseJson = function () {
var jsonText = $('#json').val();
if (!jsonText) {
$('#parsed').val('');
return;
}
try {
var type = $('input[name=jsonType]:checked').val();
console.log(type);
var json;
if (type === 'json5') {
json = JSON5.parse(jsonText);
} else {
json = JSON.parse(jsonText);
}
console.log(json);
var out = $('input[name=outType]:checked').val();
var format = $('input[name=format]:checked').val();
if (out === 'json5') {
if (format === 'unformat') {
$('#parsed').val(JSON5.stringify(json));
} else {
$('#parsed').val(JSON5.stringify(json, null, ' '));
}
} else {
if (format === 'unformat') {
$('#parsed').val(JSON.stringify(json));
} else {
$('#parsed').val(JSON.stringify(json, null, ' '));
}
}
} catch (e) {
$('#parsed').val(e);
}
};
$(document).ready(function () {
$('#json').keyup(function (e) {
parseJson();
});
$('#json').change(function () {
parseJson();
});
$('input[name=jsonType]').change(function () {
parseJson();
});
$('#reset').click(function() {
$('#json').val('');
parseJson();
});
});
// Drag & Drop
window.addEventListener("load", function () {
var drop = document.getElementById("dropArea");
drop.addEventListener("drop", function (event) {
event.preventDefault();
var filelist = event.dataTransfer.files;
for (var i = 0; i < filelist.length; i++) {
if (window.FileReader && (filelist[i].type == "text/plain")) {
var reader = new FileReader();
reader.readAsText(filelist[i]);
reader.onload = function (event) {
$('#json').val($('#json').val() + event.target.result + '\n');
parseJson();
}
drop.style.backgroundColor = "#FFF";
$('#dropResult').html('');
} else {
$('#dropResult').html('<i class="fa fa-commenting-o" aria-hidden="true"></i>Drag & Drop can only [*.txt] : ' + filelist[i].name);
drop.style.backgroundColor = "#ecc";
}
}
}, true);
drop.addEventListener("dragenter", function (event) {
event.preventDefault();
drop.style.backgroundColor = "#ccc";
}, true);
drop.addEventListener("dragleave", function (event) {
event.preventDefault();
drop.style.backgroundColor = "#ccc";
}, true);
drop.addEventListener("dragover", function (event) {
event.preventDefault();
}, true);
window.addEventListener("drop", function (event) {
event.preventDefault();
}, true);
window.addEventListener("dragenter", function (event) {
event.preventDefault();
}, true);
window.addEventListener("dragleave", function (event) {
event.preventDefault();
}, true);
window.addEventListener("dragover", function (event) {
event.preventDefault();
}, true);
}, true);
</script>
</body>
</html>