2013-05-04 17:54:03 +08:00
|
|
|
# Usage
|
|
|
|
|
|
|
|
### Install
|
|
|
|
|
2019-10-06 03:43:03 +08:00
|
|
|
Install via npm:
|
2013-05-04 17:54:03 +08:00
|
|
|
|
|
|
|
npm install jsoneditor
|
|
|
|
|
|
|
|
## Load
|
|
|
|
|
2013-05-04 18:02:20 +08:00
|
|
|
To implement JSONEditor in a web application, load the javascript and css file
|
2013-05-04 17:54:03 +08:00
|
|
|
in the head of the HTML page:
|
|
|
|
|
|
|
|
```html
|
2015-03-01 03:47:23 +08:00
|
|
|
<link href="jsoneditor/dist/jsoneditor.min.css" rel="stylesheet" type="text/css">
|
|
|
|
<script src="jsoneditor/dist/jsoneditor.min.js"></script>
|
2013-05-04 17:54:03 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
## Use
|
|
|
|
|
2019-10-06 03:43:03 +08:00
|
|
|
In the body, create a div element with an id and a size:
|
2013-05-04 17:54:03 +08:00
|
|
|
|
|
|
|
```html
|
|
|
|
<div id="jsoneditor" style="width: 400px; height: 400px;"></div>
|
|
|
|
```
|
|
|
|
|
|
|
|
After the page is loaded, load the editor with javascript:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var container = document.getElementById("jsoneditor");
|
|
|
|
var options = {
|
|
|
|
mode: 'tree'
|
|
|
|
};
|
2014-05-30 04:13:37 +08:00
|
|
|
var editor = new JSONEditor(container, options);
|
2013-05-04 17:54:03 +08:00
|
|
|
```
|
|
|
|
|
2013-05-04 18:02:20 +08:00
|
|
|
To set JSON data in the editor:
|
2013-05-04 17:54:03 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
var json = {
|
|
|
|
"Array": [1, 2, 3],
|
|
|
|
"Boolean": true,
|
|
|
|
"Null": null,
|
|
|
|
"Number": 123,
|
|
|
|
"Object": {"a": "b", "c": "d"},
|
|
|
|
"String": "Hello World"
|
|
|
|
};
|
|
|
|
editor.set(json);
|
|
|
|
```
|
|
|
|
|
2013-05-04 18:02:20 +08:00
|
|
|
To get JSON data from the editor:
|
2013-05-04 17:54:03 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
var json = editor.get();
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Full Example
|
|
|
|
|
|
|
|
```html
|
|
|
|
<!DOCTYPE HTML>
|
2020-09-23 16:23:38 +08:00
|
|
|
<html lang="en">
|
2013-05-04 17:54:03 +08:00
|
|
|
<head>
|
2015-05-15 03:25:10 +08:00
|
|
|
<!-- 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">
|
2015-05-15 03:25:10 +08:00
|
|
|
|
2015-03-01 03:47:23 +08:00
|
|
|
<link href="jsoneditor/dist/jsoneditor.min.css" rel="stylesheet" type="text/css">
|
|
|
|
<script src="jsoneditor/dist/jsoneditor.min.js"></script>
|
2013-05-04 17:54:03 +08:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p>
|
|
|
|
<button onclick="setJSON();">Set JSON</button>
|
|
|
|
<button onclick="getJSON();">Get JSON</button>
|
|
|
|
</p>
|
|
|
|
<div id="jsoneditor" style="width: 400px; height: 400px;"></div>
|
|
|
|
|
2015-02-28 04:54:04 +08:00
|
|
|
<script>
|
2013-05-04 17:54:03 +08:00
|
|
|
// create the editor
|
|
|
|
var container = document.getElementById("jsoneditor");
|
2014-05-30 04:13:37 +08:00
|
|
|
var editor = new JSONEditor(container);
|
2013-05-04 17:54:03 +08:00
|
|
|
|
|
|
|
// set json
|
|
|
|
function setJSON () {
|
|
|
|
var json = {
|
|
|
|
"Array": [1, 2, 3],
|
|
|
|
"Boolean": true,
|
|
|
|
"Null": null,
|
|
|
|
"Number": 123,
|
|
|
|
"Object": {"a": "b", "c": "d"},
|
|
|
|
"String": "Hello World"
|
|
|
|
};
|
|
|
|
editor.set(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get json
|
|
|
|
function getJSON() {
|
|
|
|
var json = editor.get();
|
|
|
|
alert(JSON.stringify(json, null, 2));
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
2013-05-04 18:02:20 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
For more examples, see the
|
|
|
|
[examples section](https://github.com/josdejong/jsoneditor/tree/master/examples).
|