JavaScript JSON
JSON is a format for storing and transmitting data.
JSON is typically used to pass data to the web server.
What is JSON?
- JSON English nameJava S cript O bject N otation
- JSON is a lightweight data interchange format.
- JSON is language independent*
- JSON easy to understand.
* JSON using JavaScript, but JSON is a text format only. The text can be read in any programming language, and as a data transfer format. |
JSON examples
The following employees JSON syntax defined objects: an array of records (objects) of three employees:
JSON Example
{ "FirstName": "John", "lastName": "Doe"},
{ "FirstName": "Anna", "lastName": "Smith"},
{ "FirstName": "Peter", "lastName": "Jones"}
]}
After JSON formatted as JavaScript objects
JSON format is syntactically create JavaScript object code is the same.
Since they are similar, JavaScript programs can easily be JSON data into JavaScript objects.
JSON syntax rules
- Data for the key / value pairs.
- Data separated by commas.
- Save Object braces
- Save the array brackets
JSON data - a name that corresponds to a value
JSON data format for the key / value pairs, like JavaScript object properties.
Key / value pairs include field names (in double quotes), followed by a colon, then the value is:
JSON object
JSON object is stored in braces.
As in JavaScript, the object can hold more key / value pairs:
JSON array
JSON array holds in brackets.
As in JavaScript, the array can contain objects:
{ "FirstName": "John", "lastName": "Doe"},
{ "FirstName": "Anna", "lastName": "Smith"},
{ "FirstName": "Peter", "lastName": "Jones"}
]
In the above example, the object "employees" is an array. It contains three objects.
Each object is recording for employees (first and last name).
JSON string is converted to a JavaScript object
Usually we read JSON data from the server, and displays the data in a Web page.
For simplicity, we set up a web page directly JSON string (you can also read our JSON tutorial ):
First, create a JavaScript string, character string data in JSON format?:
'{ "FirstName": "John", "lastName": "Doe"},' +
'{ "FirstName": "Anna", "lastName": "Smith"},' +
'{ "FirstName": "Peter", "lastName": "Jones"}]}';
Then, use the built-in JavaScript function JSON.parse () to convert a string to JavaScript objects:
Finally, using the new JavaScript object in your page:
Examples
<Script>
document.getElementById ( "demo"). innerHTML =
obj.employees [1] .firstName + "" + obj.employees [1] .lastName;
</ Script>
try it"
Related Functions
function | description |
---|---|
JSON.parse () | For a JSON string into a JavaScript object. |
JSON.stringify () | JavaScript is used to convert the value to a JSON string. |
JSON more information, you can read our JSON tutorial .