认识json
资料来自 http://www.learn-ajax-tutorial.com/Json.cfm
var Beatles = ["Paul","John","George","Ringo"];
This is the equivalent of:
等同于
var Beatles = new Array("Paul","John","George","Ringo");
JavaScript Object Notation (JSON)
JSON is a lightweight format for exchanging data between the client and server.
Json是一种在服务器端和客户端交换数据的轻量级数据格式。
It is often used in Ajax applications because of its simplicity and because its format is based on JavaScript object literals.
由于它基于js的语法格式和简洁性经常被用于ajax应用程序中。
We will start this lesson by learning JavaScript's object-literal syntax and then we will see how we can use JSON in an Ajax application.
我们将从学习js的语法开始,之后我们将明白怎样在ajax的应用程序中应用json。
Object Literals
We saw earlier how to create new objects in JavaScript with the Object() constructor.
我们很容易的明白在js中如何使用Object()构造器创建对象。
Array literals are created with square brackets as shown below:
数组值按以下方式用中括号创建
Object literals are created with curly brackets:
对象值用大括号创建
var Beatles = {
"Country" : "England",
"YearFormed" : 1959,
"Style" : "Rock'n'Roll"
}
等同于:
This is the equivalent of: var Beatles = new Object();Beatles.Country = "England";
Beatles.YearFormed = 1959;
Beatles.Style = "Rock'n'Roll";
Just as with all objects in JavaScript, the properties can be references using dot notation or bracket notation.
如同js中的所有对象一样,熟悉可以用逗号和方括号引用
alert(Beatles.Style); //Dot Notation
alert(Beatles["Style"]); //Bracket Notation
Arrays in Objects
对象可以包含数组
Object literals can contain array literals:
{
"Name" : "Beatles",
"Country" : "England",
"YearFormed" : 1959,
"Style" : "Rock'n'Roll",
"Members" : ["Paul","John","George","Ringo"]
},
{
"Name" : "Rolling Stones",
"Country" : "England",
"YearFormed" : 1962,
"Style" : "Rock'n'Roll",
"Members" : ["Mick","Keith","Charlie","Bill"]
}
]
JSON
在http://www.json.org Json被描述为:
1 一种轻量级的数据交换格式
2 容易的读写
3 使机器容易生出和解析
Json
On the JSON website (http://www.json.org), JSON is described as:
1. a lightweight data-interchange format
2. easy for humans to read and write
3. easy for machines to parse and generate
The JSON syntax is like JavaScript's object literal syntax except that the objects cannot be assigned to a variable. JSON just represents the data itself.
除了对象值不能赋值于一个变量以外,json的语法与js的语法很相似。Json仅仅描述它自己的数据
JSON Parsers
As JSON is just a string of text and not an object in and of itself, it needs to be converted to an object before to make it useful.
Although this can be done in JavaScript with the eval() function, it is safer to use a JSON parser.
Json只是一串文字,而不是一个对象,它需要转换成对象才会变的有用。
虽然在js中可以使用eval()函数实现转换,但是使用json parser会更安全一些。你可以在http://www.json.org/json.js下载JSON parser
You can download the JavaScript JSON parser at http://www.json.org/json.js.
你可以在http://www.json.org/json.js下载JSON parser
下面我们将介绍客户端和服务器端的数据通信:
The process for sending data between the browser and server with JSON is as follows:
1. On the client-side:
o Create a JavaScript object using the standard or literal syntax.
o Use the JSON parser to stringify the object.
o Send the URL-encoded JSON string to the server as part of the HTTP Request. This can be done using the HEAD, GET or POST method by assigning the JSON string to a variable. It can also be sent as raw text using the POST method, but this may create extra work for you on the server-side.
2. On the server-side:
o Decode the incoming JSON string and convert the result to an object using a JSON parser for the language of your choice. At http://www.json.org, you'll find JSON parsers for many modern programming languages. The methods available depend upon which parser you are using. See the parser's documentation for details.
o Do whatever you wish with the object.
o If you wish to send JSON back to the client:
§ Create a new object for storing the response data.
§ Convert the new object to a string using your JSON parser.
§ Send the JSON string back to the client as the response body (e.g, Response.Write(strJSON), echo $strJSON, out.write(strJSON) etc.).
3. On the client-side:
o Convert the incoming JSON string to an object using the JavaScript JSON parser.
o Do whatever you wish with the object.
o And so on...
在客户端:
1) 用标准的语法创建js对象
2) 用 JSON parser处理对象
3) 作为http请求的一部分向服务器端发送url编码过的json字符串,可以将json赋值给一个原始变量用head,get或者post方法完成。也可以使用post方法发送原始的文字,但是这样会为服务器端增加额外的工作。
在服务器端
1) 解码json字符串,使用json parser和你使用的语言将其转换为一个对象。http://www.json.org,上你将找到许多现代编程语言装备的json转换器。方法的可用性取决于你所选用的语言。更多细节可以参看文档。
2) 使用对象进行操作
3) 如果你想返回客户端json数据
1)创建一个用于存储输出数据的对象
2)使用json parser将新对象转换为字符串
3) 向客户端发送json字符串
在客户端:
1) 将json解析为js对象
2) 操作对象
3) 或者其它