const placeholder = {
string: 'hello world!',
boolean: true,
color: '#6c928c',
number: 123,
null: null,
array: [1, 2, 3],
object: {a: 'b', c: 'd', e: 'f'}
};
const json_editor = new JSONEditor(document.getElementById("json_editor"), {mode: 'code'}, placeholder);
const tree_editor = new JSONEditor(document.getElementById("tree_editor"), null, placeholder);
tree_editor.expandAll();
function toTree() {
tree_editor.set(JSON.parse(json_editor.getText()));
tree_editor.expandAll();
}
function toJson() {
json_editor.setText(JSON.stringify(tree_editor.get(), null, 2))
}
实现效果
~~~
在线DEMO(https://oktools.net/json)