`
yhw
  • 浏览: 3204 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
最近访客 更多访客>>
社区版块
存档分类
最新评论

AJAX in the transfer of data to another model:JSON

    博客分类:
  • ajax
阅读更多

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.

分享到:
评论

相关推荐

    JavaScript.JSON.Cookbook.1785286900

    This is especially true if you've been using XML to do the job because it's entirely possible that you could do much of the same work with less code and less data overhead in JSON. While the book's ...

    Beginning.JSON.1484202031

    Beginning JSON is the definitive guide to JSON - JavaScript Object Notation - today’s standard in data formatting for the web. The book starts with the basics, and walks you through all aspects of ...

    AjaxJson 实例 AjaxJson

    AjaxJson 是一种在 Web 应用程序中实现异步数据交换的技术,它结合了 AJAX(Asynchronous JavaScript and XML)和 JSON(JavaScript Object Notation)的优势,使得网页可以在不刷新整个页面的情况下与服务器进行...

    Ajax开发实践:JSON在XMLHttpRequest中的应用

    在Ajax开发实践中,JSON(JavaScript Object Notation)在XMLHttpRequest中的应用是一个关键点,它极大地提高了数据传输的效率和用户体验。 XMLHttpRequest是Ajax的核心组件,它允许JavaScript与服务器进行异步通信...

    Ajax调用restful接口传送Json格式数据的方法

    ajax传送json格式数据,关键是指定contentType,data要是json格式 如果是restful接口,把type改成对应的post(增)、delete(删)、put(改)、get(查)即可 var post_data={"name":"test001","pass":"xxxx"}; $....

    掌握 Ajax第 10 部分--使用 JSON 进行数据传输.pdf

    ### 掌握 Ajax 第 10 部分——使用 JSON 进行数据传输 #### 知识点一:理解 JSON 在 Ajax 中的作用 - **背景介绍**:随着 Web 应用程序的发展,Ajax(Asynchronous JavaScript and XML)技术成为了前端开发的重要...

    AJAX和struts2传递JSON数组

    这里,`data`是我们的JSON数组,`contentType`设置为`application/json`表明我们正在发送JSON数据,`JSON.stringify()`用于将JavaScript对象序列化为JSON字符串。 **二、后端Struts2 Action接收JSON** 在Struts2的...

    Modelling and Generating Ajax Applications A Model-Driven Approach

    In conclusion, the paper "Modelling and Generating AJAX Applications: A Model-Driven Approach" highlights the need for a new modeling methodology tailored to AJAX applications. By adopting MDA ...

    ajax解析json实例

    当AJAX请求成功后,服务器返回的JSON数据会在success回调函数的data参数中。我们可以使用JavaScript的内置方法`JSON.parse()`将JSON字符串转换为JavaScript对象: ```javascript success: function(data) { var ...

    AjaxJson实体类与依赖包

    在IT行业中,Ajax和JSON是两个非常重要的概念,它们在构建动态、响应式的Web应用程序时起着关键作用。Ajax(Asynchronous JavaScript and XML)是一种在无需刷新整个网页的情况下,能够更新部分网页的技术。而JSON...

    Ajax中使用JSON传输数据

    在Ajax中使用JSON作为数据传输格式,可以实现高效、简洁的数据交换。 **JSON的优势:** 1. **易于理解**:JSON格式基于JavaScript语法,因此对于JavaScript开发者来说,理解和解析JSON非常直观。 2. **小巧高效**:...

    ajax+json实例

    **Ajax+JSON 实例详解** 在现代Web开发中,AJAX(Asynchronous JavaScript and XML)技术已经成为提升用户体验的重要工具,它允许网页在不刷新整个页面的情况下与服务器进行数据交互。结合JSON(JavaScript Object ...

    Ajax:拥抱JSON,让XML走开

    ### Ajax:拥抱JSON,让XML走开 #### Ajax与异步通信 Ajax(Asynchronous JavaScript and XML)是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。它结合了多种技术,包括JavaScript、...

    springMVC+ajax+json

    Spring MVC、Ajax 和 JSON 是现代Web开发中的关键技术,它们共同构建了高效、动态的用户界面。下面将详细解释这三个技术及其在实际应用中的结合。 **Spring MVC** Spring MVC 是 Spring 框架的一部分,用于构建基于...

    Data.Collection.with.R.A.Practical.Guide.to.Web.Scraping.and.Text.Mining

    An extensive set of exercises are presented to guide the reader through each technique. Explores both supervised and unsupervised techniques as well as advanced techniques such as data scraping and ...

    什么是Ajax和json???

    ### 什么是Ajax和JSON? #### 1. Ajax详解 ##### 1.1 定义Ajax **Ajax**(Asynchronous JavaScript and XML)是一组技术的集合,这些技术结合在一起能够实现浏览器端对用户请求的异步处理,从而提高用户体验。...

    SpringMVC利用Ajax,JQuery交互Json

    要向服务器发送Json数据,可以在Ajax请求中设置`data`属性,例如: ```javascript var sendData = { key1: 'value1', key2: 'value2' }; $.ajax({ url: '/saveJson', type: 'POST', contentType: '...

    ajax返回json对象.zip

    "Ajax返回JSON对象"这个主题聚焦于Ajax如何处理JSON(JavaScript Object Notation)数据格式,这是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。 1. **Ajax基础**: - **异步通信**:...

Global site tag (gtag.js) - Google Analytics