JSON data storage format for Ajax interaction with users, ajaxjson

Source: Internet
Author: User

JSON data storage format for Ajax interaction with users, ajaxjson

Data storage is the core function of JavaScript, which is a confusing problem in the early stages of learning. It is not an eye-catching effect, such as page sliding, slide display, and fade-in and fade-out. Properly storing the data helps us organize the structure and make it easier for applications to access the content later. JavaScript provides various data storage methods. It is an evolutionary model from basic to the future:

XML stands for the extensible markup language. It is a flexible data format that many applications use to store data. Its structure is like HTML and contains elements, tags, and attributes, all models are the same. One of its major advantages is that it is a scalable format, and you are not subject to a predefined data structure; and it also complies with DOM standards, the data obtained by calling AJAX is similar to parsing HTML and parsed through the corresponding DOM method and attribute:

<ingredient><prison><name>apple</name><color>red</color></prison><prison><name>tomato</name><color>red</color></prison><prison><name>peach</name><color>pink</color></prison><prison><name>pitaya</name><color>green</color></prison></ingredient>

However, XML cannot be used for cross-domain Aiax calls, which means that the XML data of each Ajax call must be in the same domain name as the script for executing the call, otherwise, the request will fail.

HTML storage is similar. It is the easiest to combine with Ajax. Speed is an important factor in using this data format. HTML code segments can be directly output to the DOM without parsing on the client, therefore, we do not need to write JavaScript code to parse HTML.

<ul><li><a href="boke.js">apple:red</a></li><li>tomato:red</li><li>peach:pink</li><li>piyata:green</li></ul>

Let's talk about its disadvantages. The data stored in it may be mixed with HTML code, so maintainability is reduced. In addition, developers may need to perform some fine-grained control measures on the server side when writing and processing code.

The following describes how to store data using JavaScript...

Variable

JavaScript is the most basic data storage method. It accepts strings, values, and boolean values. The core content is its scope.

Performance issues related to variables:

After the variables are declared, they will be cached in the JavaScript file, so they can be used repeatedly in the scope, and the overhead of getting variables is minimal, as long as the data usage exceeds 1, it should be saved to the variable.

Array

Arrays and common arrays, associated arrays, and multi-dimensional arrays are all element lists. This form is very flexible in various data storage. Having mastered the most basic form of array, it will also be very helpful for the use of other complex forms. The access integration of array elements is a common basic interactive code. In view of the importance of the array data storage type, JavaScript provides a lot of exclusive methods for Array interaction operations:

. The join () method can specify a character as the connector for each element in the array. The slice () method can output elements within a certain range in the array; shift () and unshift () the methods are used to remove or add new elements in the array header. The pop () method is used to remove and return the last element in the array. The concat () method is used to concatenate multiple arrays into an array, the sort () method can sort the elements of an array in alphabetical or custom order, but only sorts the elements by the first character, which is generally incorrect.

Object

When the data becomes more complex and difficult to manage, I will choose to convert the array to an object. Because the object storage method is easier to read.

// Object Data Storage var meatball = {bread: "wang", meat: "li", cheese: "zhang"}; // return the bread alert (meatball. bread );

Impact of objects on Efficiency

The above code has only one layer, but it can be infinitely nested. Objects can better organize code and improve modularity. But at the same time, we still need to consider the trade-off between performance and maintainability, because it needs to find one of the three. If object-oriented programming technology is used to nest three or more layers, it will lead to performance problems, but this is not the case when used on the server side, therefore, front-end developers must decide when to use object-oriented programming technology.

JSON

JSON is a data format that is easy to integrate with jacascript. It is usually used when JavaScript is used to call external servers. JSON data is stored in independent files, and is usually stored on another completely different server. It is now the most common data format used to call API services. It is selected because it is easy to read. At first, JSON format emerged as an alternative to XML format, but soon it became the mainstream in the field of data exchange. It is a lightweight data format that is easy to access across domain names through remote Ajax calls. Although JavaScript provides native support for parsing JSON format, it is still a platform-independent format that can be used by clients and servers during data exchange.

Because the JSON format supports cross-domain access, to prevent all malicious behaviors, we must only retrieve JSON from trusted data sources.

This is a Salad. json data file:

//ajax JSON Saladvar ingredient = {"fruit":[{"name" : "apple","color" : "green"},{"name" : "tomato","color" : "red"},{"name" : "peach","color" : "pink"},{"name" : "pitaya","color" : "white"},{"name" : "lettuce","color" : "green"}]};

JSON is most commonly used in combination with API calls. It is used in the API-based network program development environment. The data format does not need to be changed.

The above section describes the JSON data storage format for Ajax interaction with users. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.