THE WORLD'S LARGEST WEB DEVELOPER SITE

JS教程

JS主页 JS简介 JS在哪里 JS输出 JS语句 JS语法 JS评论 JS变量 JS运算符 JS算术 JS分配 JS数据类型 JS函数 JS对象 JS活动 JS字符串 JS字符串方法 JS号 JS编号方法 JS数组 JS数组方法 JS数组排序 JS数组迭代 JS日期 JS日期格式 JS日期获取方法 JS日期设置方法 JS数学 JS 随机 JS布尔值 JS比较 JS条件 JS开关 JS循环 JS循环播放 JS Break JS类型转换 JS运算 JS正则 JS错误 JS范围 JS吊装 JS严格模式 JS此关键字 JS Let JS Const JS箭头功能 JS调试 JS样式指南 JS最佳做法 JS错误 JS性能 JS保留字 JS版本 JS ES5版 JS ES6版 JS JSON

JS表单

JS表单 Forms API

JS对象

对象定义 对象属性 对象方法 对象显示 对象访问器 对象构造器 对象原型 对象ECMAScript 5 Object Classes

JS函数

功能定义 功能参数 函数调用 函数调用 应用功能 功能闭包

JS HTML DOM

DOM简介 DOM方法 DOM文档 DOM元素 DOM HTML DOM CSS DOM动画 DOM事件 DOM事件监听器 DOM导航 DOM节点 DOM集合 DOM节点列表

JS浏览器BOM

JS窗口 JS屏幕 JS位置 JS历史记录 JS导航器 JS弹出警报 JS计时 JS Cookies

JS AJAX

AJAX简介 AJAX XMLHttp AJAX请求 AJAX响应 AJAX XML文件 AJAX PHP AJAX ASP AJAX数据库 AJAX应用程序 AJAX 示例

JS JSON

JSON简介 JSON语法 JSON与XML JSON数据类型 JSON解析 JSON Stringify JSON对象 JSON数组 JSON PHP JSON HTML JSON JSONP

JS Web APIs

Web API Intro Web History API Web Storage API Web Geolocation API

JS vs jQuery

jQuery选择器 jQuery HTML jQuery CSS jQuery DOM

JS 示例

JS 示例 JS HTML DOM JS HTML输入 JS HTML对象 JS HTML事件 JS浏览器 JS编辑器 JS 练习 JS测验 JS证书

JS 参考

JavaScript对象 HTML DOM对象


JSON - Introduction

JSON: JavaScript Object Notation.

JSON is a syntax for storing and exchanging data.

JSON is text, written with JavaScript object notation.


Exchanging Data

When exchanging data between a browser and a server, the data can only be text.

JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server.

We can also convert any JSON received from the server into JavaScript objects.

This way we can work with the data as JavaScript objects, with no complicated parsing and translations.


Sending Data

If you have data stored in a JavaScript object, you can convert the object into JSON, and send it to a server:

Example

var myObj = {name: "John", age: 31, city: "New York"};
var myJSON = JSON.stringify(myObj);
window.location = "demo_json.php?x=" + myJSON;
来试试吧 »

You will learn more about the JSON.stringify() function later in this tutorial.


Receiving Data

If you receive data in JSON format, you can convert it into a JavaScript object:

Example

var myJSON = '{"name":"John", "age":31, "city":"New York"}';
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.name;
来试试吧 »

You will learn more about the JSON.parse() function later in this tutorial.



Storing Data

When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats.

JSON makes it possible to store JavaScript objects as text.

Example

Storing data in local storage

// Storing data:
myObj = {name: "John", age: 31, city: "New York"};
myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);

// Retrieving data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;
来试试吧 »

什么是JSON?

  • JSON stands for JavaScript Object Notation
  • JSON is a lightweight data-interchange format
  • JSON is "self-describing" and easy to understand
  • JSON is language independent *

*
JSON uses JavaScript syntax, but the JSON format is text only.
Text can be read and used as a data format by any programming language.

The JSON format was originally specified by Douglas Crockford.


Why use JSON?

Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by any programming language.

JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects:

JSON.parse()

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.




W3Schools is optimized for learning, testing, and training. 示例 might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2020 by Refsnes Data. All Rights Reserved.
Powered by W3.CSS.

W3Schools.com