jsoneditor/docs/usage.md

138 lines
3.1 KiB
Markdown
Raw Normal View History

2013-05-04 17:54:03 +08:00
# Usage
### Install
with npm:
npm install jsoneditor
with bower:
2013-05-04 18:09:14 +08:00
bower install jsoneditor
2013-05-04 17:54:03 +08:00
2013-05-04 18:02:20 +08:00
download:
2013-05-04 17:54:03 +08:00
2013-05-04 18:02:20 +08:00
[http://jsoneditoronline.org/downloads/](http://jsoneditoronline.org/downloads/)
2013-05-04 17:54:03 +08:00
The library consists of three files: one javascript file, one css file and an
image. Both full and minified version are available.
## 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
<link rel="stylesheet" type="text/css" href="jsoneditor/jsoneditor.min.css">
<script type="text/javascript" src="jsoneditor/jsoneditor.min.js"></script>
2013-05-04 17:54:03 +08:00
```
### Detailed error messages
Optionally, [jsonlint](https://github.com/zaach/jsonlint) can be loaded to get
more detailed error messages.
```html
<script type="text/javascript" src="jsoneditor/asset/jsonlint/jsonlint.js"></script>
2013-05-04 17:54:03 +08:00
```
### Code editor
2013-05-04 18:02:20 +08:00
The mode 'code' requires the [Ace editor](http://ace.ajax.org/) to be loaded.
JSON Editor comes with a custom built version of Ace containing the ace modules
`ace.js`, `ext-searchbox.js`, `mode-json.js`, `theme-textmate.js`, and a custom
theme `theme-jsoneditor.js`.
Besides loading ace, the content type must be specified on the page.
2013-05-04 17:54:03 +08:00
```html
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="jsoneditor/asset/ace/ace.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");
var options = {
mode: 'tree'
};
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>
<html>
<head>
<link rel="stylesheet" type="text/css" href="jsoneditor/jsoneditor.min.css">
<script type="text/javascript" src="jsoneditor/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>
<script type="text/javascript" >
// create the editor
var container = document.getElementById("jsoneditor");
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).