2018-07-28 20:21:12 +08:00
|
|
|
<!DOCTYPE HTML>
|
2020-09-23 16:23:38 +08:00
|
|
|
<html lang="en">
|
2018-07-28 20:21:12 +08:00
|
|
|
<head>
|
|
|
|
<!-- when using the mode "code", it's important to specify charset utf-8 -->
|
2020-09-23 16:23:38 +08:00
|
|
|
<meta charset="utf-8">
|
|
|
|
|
|
|
|
<title>JSONEditor | Update JSON</title>
|
2018-07-28 20:21:12 +08:00
|
|
|
|
|
|
|
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
|
|
|
<script src="../dist/jsoneditor.js"></script>
|
|
|
|
|
|
|
|
<style type="text/css">
|
|
|
|
body, html {
|
|
|
|
font: 10.5pt arial;
|
|
|
|
color: #4d4d4d;
|
|
|
|
line-height: 150%;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.main {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.main > div {
|
|
|
|
margin: 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
#jsoneditor {
|
|
|
|
flex: 1;
|
|
|
|
width: 100%;
|
|
|
|
max-width: 500px;
|
|
|
|
height: 200px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="main">
|
|
|
|
<div>
|
|
|
|
<button id="update">update JSON</button>
|
2018-08-06 16:27:57 +08:00
|
|
|
<button id="updateText">update JSON Text</button>
|
2018-07-28 20:21:12 +08:00
|
|
|
</div>
|
|
|
|
<div id="jsoneditor"></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
var container = document.getElementById('jsoneditor');
|
|
|
|
var updateButton = document.getElementById('update');
|
2018-08-06 16:27:57 +08:00
|
|
|
var updateTextButton = document.getElementById('updateText');
|
2018-07-28 20:21:12 +08:00
|
|
|
|
|
|
|
var options = {
|
|
|
|
mode: 'tree',
|
2019-07-09 02:00:34 +08:00
|
|
|
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'] // allowed modes
|
2018-07-28 20:21:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var json = {
|
|
|
|
"arrayToObject": [1, 2, 3],
|
|
|
|
"arrayGrow": [1, 2, 3],
|
|
|
|
"arrayShrink": [1, 2, 3],
|
|
|
|
"autoToArray": 123,
|
2018-07-30 04:00:15 +08:00
|
|
|
"arrayToAuto": [1, 2, 3],
|
2018-07-28 20:21:12 +08:00
|
|
|
"objectGrow": {"a": "b", "c": "d"},
|
|
|
|
"objectShrink": {"a": "b", "c": "d"},
|
|
|
|
"objectToArray": {"a": "b", "c": "d"},
|
|
|
|
"removeField": "old"
|
|
|
|
};
|
|
|
|
|
|
|
|
var updatedJson = {
|
|
|
|
"arrayToObject": {"a": "b", "c": "d"},
|
|
|
|
"arrayGrow": [1, 2, 3, 4, 5],
|
|
|
|
"arrayShrink": [1, 3],
|
2018-07-30 04:00:15 +08:00
|
|
|
"autoToArray": [1, 2, 3],
|
|
|
|
"arrayToAuto": 123,
|
2018-07-28 20:21:12 +08:00
|
|
|
"objectGrow": {"a": "b", "c": "ddd", "e": "f"},
|
|
|
|
"objectShrink": {"c": "d"},
|
|
|
|
"objectToArray": [1, 2, 3],
|
|
|
|
"newField": "new"
|
|
|
|
};
|
|
|
|
|
|
|
|
var editor = new JSONEditor(container, options, json);
|
|
|
|
|
|
|
|
editor.expandAll();
|
|
|
|
|
|
|
|
updateButton.onclick = function () {
|
|
|
|
editor.update(updatedJson);
|
2018-08-06 16:27:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
updateTextButton.onclick = function () {
|
|
|
|
editor.updateText(JSON.stringify(updatedJson, null, 2));
|
|
|
|
};
|
2018-07-28 20:21:12 +08:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|