Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

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.

Note * 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

{ "Employees": [
{ "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:

"FirstName": "John"


JSON object

JSON object is stored in braces.

As in JavaScript, the object can hold more key / value pairs:

{ "FirstName": "John", "lastName": "Doe"}


JSON array

JSON array holds in brackets.

As in JavaScript, the array can contain objects:

"Employees": [
{ "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?:

var text = '{ "employees": [' +
'{ "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:

var obj = JSON.parse (text);

Finally, using the new JavaScript object in your page:

Examples

<P id = "demo"> </ p>

<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 .