forked from DavidDurman/FlexiJsonEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsoneditor.js
61 lines (50 loc) · 1.44 KB
/
jsoneditor.js
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
var json = {
"string": "foo",
"number": 5,
"array": [1, 2, 3],
"object": {
"property": "value",
"subobj": {
"arr": ["foo", "ha"],
"numero": 1
}
}
};
function printJSON() {
$('#json').val(JSON.stringify(json));
}
$(document).ready(function() {
$('#rest > button').click(function() {
var url = $('#rest-url').val();
$.ajax({
url: url,
dataType: 'jsonp',
jsonp: $('#rest-callback').val(),
success: function(data) {
json = data;
$('#editor').jsonEditor(json, { change: printJSON });
printJSON();
},
error: function() {
alert('Something went wrong, double-check the URL and callback parameter.');
}
});
});
$('#json').change(function() {
var val = $('#json').val();
if (val) {
try { json = JSON.parse(val); }
catch (e) { alert('Error in parsing json. ' + e); }
} else {
json = {};
}
$('#editor').jsonEditor(json, { change: printJSON });
});
$('#expander').click(function() {
var editor = $('#editor');
editor.toggleClass('expanded');
$(this).text(editor.hasClass('expanded') ? 'Collapse' : 'Expand all');
});
printJSON();
$('#editor').jsonEditor(json, { change: printJSON });
});