AJAX in the transfer of data to another model:JSON

Ajax is "asynchronous javascript and XML" has been well-known acronym,However,

although XML is an important part of the look,It isn't necessary.A senior software

engineer Douglas Crock ford developed a built-in javascript data format,That the

object known as the javascript(JSON,Javascript Object Notation),Means that Ajax

object to the use of direct transmission of information,Can be read as "Jason".

 

1.What is JSON
  The concept is simple JSON,JSON is a lightweight data format,It javascript-based

subset of the grammar,That is, and the array of objects that.As a result of the

use of grammar is javascript,Therefore, the definition of JSON can be included in

the javascript file,Their visit without the need for XML-based language for

additional analysis.However, before the use of JSON,It is important to understand

the javascript array and the object literally in the amount of the special syntax.

 

1.1Array literal volume
   Array literal volume,The other one is from a group of brackets separated by the

value of the javascript,Example:
   var aNames=["hello", 12, true , null];

 

1.2Literal target volume
   Literal target volume,Is to spend two brackets to the definition of.Flowers in

the brackets can be placed any number of "name - the value",Definition Format

String Value.In addition to the last line outside,Each "name - the value of" there

must be a comma after(Perl and in this joint array similar to the definition of

some).Example:
   var oCar = {
               "color": "red",      
              "doors" : 4,
               "paidFor" : true
        };

 

1.3Mixed literal volume
   We can mix and the object literally array volume,To create an object array,Or

contains an array of objects.Example:
   {comments:[
{
id:1,
author:"someone1",
url:"http://someone1.x2design.net",
content:"hello"
},
{
id:2,
author:"someone2",
url:"http://someone2.x2design.net",
content:"hello"
},
{
id:3,
author:"someone3",
url:"http://someone3.x2design.net",
content:"hello"
}
]};

 

1.4JSON syntax
   Ajax in applications,Server is a direct statement to generate javascript,Direct

access to the client after the eval method used to obtain this target, so that

might dispense with the analysis of XML performance loss. At the same time, the

use of javascript communications JSON data format as the star of the benefits are,

you can immediately access the data value, the faster the access, including data.
    var oCarInfo = eval("(" + sJSON + ")");
   Remember: In the javascript in brackets spent a statement. Resolver to know

that the curly braces is a target rather than a statement of whether it is the

only way to find it in parentheses package (which is used to describe the code is

an expression rather than a statement).

 

1.5JSON encoding and decoding
   JSON as part of the resources, Corockford developed a JSON to achieve the

target directly and Javascript coding and decoding. The source tools can

www.crockford.com / JSON / json.js download.        
   Used in the above eval () there are some inherent shortcomings: it is used for

the Javascript code into any evaluation and not just the JSON. As a result, when

it comes to enterprise-class web application development, it's a big security

risk. To solve this problem, you can use JSON code will only be used to convert

the Javascript parser JSON.parse () methods to achieve. Example:
         var oCar = new Object();
       oCar.doors = 4;
        oCar.color = "blue";
       oCar.year = 1995;
       oCar.drivers = new Array("Penny", "Dan" , "Kris");
       document.write(JSON.stringify(oCar));
   This code will be output of the JSON string is as follows:
   {"doors" : 4, "color" : "blue", "year" :1995, "drivers" : ["Penny", "Dan" ,

"Kris"]}

 


2.JSON and XML
  As mentioned above, JSON and XML compared to one of the major advantages is that

it more simple.
  See XML data example: 
  The use of XML, said:
<comments>
<comment>
<id>1</id>
<author>someone1</author>
<url>http://someone1.x2design.net</url>
<content>hello</content>
</comment>
<comment>
<id>2</id>
<author>someone2</author>
<url>http://someone2.x2design.net</url>
<content>someone1</content>
</comment>
<comment>
<id>3</id>
<author>someone3</author>
<url>http://someone3.x2design.net</url>
<content>hello</content>
</comment>
</comments>


The use of JSON:
{comments:[
{
id:1,
author:"someone1",
url:"http://someone1.x2design.net",
content:"hello"
},
{
id:2,
author:"someone2",
url:"http://someone2.x2design.net",
content:"hello"
},
{
id:3,
author:"someone3",
url:"http://someone3.x2design.net",
content:"hello"
}
]};

Can easily find that many of the redundant information missing. As a result there

is no need to start with the label (opening tag) to match the end of the label

(closing tag), therefore, to send the same information needed to significantly

reduce the number of bytes. Corockford its founder called "XML weight loss

program").          JSON and XML data formats, for the layman readability shortcomings

is even worse. Of course, there is a point of view is that the data exchange

format is not observed with the naked eye. If the tool back and forth through the

transmission of data to create and resolve, then indeed there is no reason for

people to make the data must be easy to read. The essence of the problem lies:

there are tools available JSON. 

 


3. Server-side JSON tools  java:
java JSON tools, developed by Douglas Crock ford, in
www.crockford.com/JSON/java/

            Download, it can be used in JSP. 

 

 

4. JSON the strengths and weaknesses        
JSON is not only a reduction of XML resolution to resolve the problem brought

about by the performance and compatibility issues, but also for the javascript is

very easy to use, can easily traverse through an array of objects, as well as

access to property data acquisition, which also Readability good, with basic The

structured nature of the data. Have to say is a good way to google maps and in

fact there will be no transmission of data using XML, JSON instead of using the

program.         JSON Another advantage is the feasibility of cross-domain, for

example, the page you www.xxx.com use is entirely feasible, which means you can

cross-border transmission of information. And the use of XMLHttpRequest is not

cross-domain access to information, javascript This is the nature of the internal

security restrictions.        
JSON looks beautiful, is not able to completely replace this XML? Is not the case,

and the reason is that the advantages of XML: versatility. To have a server-side

grammar qualified javascript code is not easy, mainly took place in relatively

large system, server and client have different developers. They must consult the

target format, which is likely to result in errors.        
In any case, JSON is an attractive technology, prepared to do a lot of trial. I

hope we can obtain great performance.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值