About the result of a non-uniform traversal of the JSON string browser output in JavaScript

Source: Internet
Author: User

We often use JavaScript json when we are doing projects.

First of all, what is the JSON string for JavaScript, the JSON string is an object of JavaScript, and there are objects that correspond to the keys and values.

The General format is:

A = {

A1:1,

A2: ' abc ',

A3: ' abc ',

A4: [A]

A5:function () {Console.log (12)}

};

The way we read this JSON is through a for-in loop;

The advantage of using the JSON string is that the data in the database can be read out of a single time in JSON form, and then read the inside through JavaScript, which can greatly reduce the number of requests to the server, increase the load efficiency of the foreground page.

We have a lot of problems when we traverse JSON, and the problem I have today is that when we write the key value of the JSON string as a number, we will not output the JSON structure in the main browser when we are traversing it, but we will output it in the order of the number from small to large. But IE8 the following browsers will output in the order we output.

For example:

A = {' 1 ': 1,' 5 ': function () {Console.log (12)},' 2 ': ' abc ',' 4 ': [The]' 3 ': {' 5 ': ' abc ', ' 6 ': ' BCD '},};

Through the For In loop

for (var i in a) {Console.log (a[i]);}

Chrome,firefox Browser output: 1,ABC,Object { 5="abc", 6="BCD" },[1, 2, 3],function ();

IE8 and the following browsers: 1,function (), ABC,[1, 2, 3], Object { 5="abc", 6="BCD"};

There are two ways to solve this problem:

The first is to change the key value of the number into a string, including letters or underscores;

The second is to solve the compatibility of IE, by first traversing the JSON and then storing the key values in an array, and then iterating through the array to get the data in the JSON.

Specific code:

var arr = [],sortnumber = function (A, b) {

return a-B;};for (var i in a) {arr[arr.length] = a[i];}

Arr.sort (Sortnumber);

for (var x = 0; x < arr.length; × x + +) {

Console.log (A[arr[x]);

}

So that the browser read the data will be consistent, there is a drawback is that this will not follow our output of the JSON format output, only to solve the browser compatibility problem.


About the result of a non-uniform traversal of the JSON string browser output in JavaScript

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.