jsoneditor/docs/usage.md

105 lines
2.0 KiB
Markdown
Raw Permalink Normal View History

2013-05-04 17:54:03 +08:00
# Usage
### Install
using npm:
2013-05-04 17:54:03 +08:00
npm install jsoneditor
## Load
To implement JSONEditor in a web application, load the javascript file
2013-05-04 17:54:03 +08:00
in the head of the HTML page:
```html\
<script src="jsoneditor/dist/jsoneditor.js"></script>
2013-05-04 17:54:03 +08:00
```
## Use
In the body, create an div element with an id and a size:
```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")
2013-05-04 17:54:03 +08:00
var options = {
mode: 'tree'
}
var editor = 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 17:54:03 +08:00
```
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()
2013-05-04 17:54:03 +08:00
```
## Full Example
```html
<!DOCTYPE HTML>
<html>
<head>
<!-- when using the mode "code", it's important to specify charset utf-8 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script src="jsoneditor/dist/jsoneditor.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>
2013-05-04 17:54:03 +08:00
</p>
<div id="jsoneditor" style="width: 400px; height: 400px;"></div>
<script>
2013-05-04 17:54:03 +08:00
// create the editor
var container = document.getElementById('jsoneditor')
var editor = 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)
2013-05-04 17:54:03 +08:00
}
// get json
function getJSON() {
var json = editor.get()
alert(JSON.stringify(json, null, 2))
2013-05-04 17:54:03 +08:00
}
</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).