赛派号

护肤品用大牌还是微商产品 What is JSON

What is JSON? ❮ Previous Next ❯ JSON

JSON stands for JaScript Object Notation

JSON is a lightweight format for storing and transporting data

JSON is often used when data is sent from a server to a web page

JSON is "self-describing" and easy to understand

JSON Example

This example defines an employees object: an array of 3 employee records (objects):

{"employees":[     {"firstName":"John", "lastName":"Doe"},     {"firstName":"Anna", "lastName":"Smith"},     {"firstName":"Peter", "lastName":"Jones"} ]}

Learn JSON Now!

JSON Syntax Rules Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold arrays JaScript Object Notation

The JSON format is syntactically identical to the code for creating JaScript objects.

Because of this similarity, a JaScript program can easily convert JSON data into native JaScript objects.

The JSON syntax is derived from JaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.

JSON Data - A Name and a Value

JSON data is written as name/value pairs, just like JaScript object properties.

A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:

"firstName":"John"

JSON names require double quotes. JaScript names do not.

JSON Objects

JSON objects are written inside curly braces.

Just like in JaScript, objects can contain multiple name/value pairs:

{"firstName":"John", "lastName":"Doe"} JSON Arrays

JSON arrays are written inside square brackets.

Just like in JaScript, an array can contain objects:

"employees":[     {"firstName":"John", "lastName":"Doe"},     {"firstName":"Anna", "lastName":"Smith"},     {"firstName":"Peter", "lastName":"Jones"} ]

In the example above, the object "employees" is an array. It contains three objects.

Each object is a record of a person (with a first name and a last name).

Converting a JSON Text to a JaScript Object

A common use of JSON is to read data from a web server, and display the data in a web page.

For simplicity, this can be demonstrated using a string as input.

First, create a JaScript string containing JSON syntax:

var text = '{ "employees" : [' + '{ "firstName":"John" , "lastName":"Doe" },' + '{ "firstName":"Anna" , "lastName":"Smith" },' + '{ "firstName":"Peter" , "lastName":"Jones" } ]}';

Then, use the JaScript built-in function JSON.parse() to convert the string into a JaScript object:

var obj = JSON.parse(text);

Finally, use the new JaScript object in your page:

Example

document.getElementById("demo").innerHTML = obj.employees[1].firstName + " " + obj.employees[1].lastName;

Try it Yourself »

Full JSON Tutorial

This has been a short description of JSON.

For a full JSON tutorial go to W3Schools JSON Tutorial.

❮ Previous Next ❯ ★ +1   Track your progress - it's free!   Log in Sign Up

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了