Inconsistent output results of JavaScript traversal of Json string browsers

Source: Internet
Author: User
Tags javascript eval
The output results of JavaScript traversal of Json strings are inconsistent with those of the browser. Let's talk about what json is before introducing the body.

JSON (JavaScript Object Notation) is a lightweight data exchange format, which is called JavaScript Object Notation. One of the advantages of using JSON for data transmission is that JSON is actually JavaScript. It is based on a text format of the subset of the literal Syntax of JavaScript objects in ECMAScript 3rd. This means that you can use responseText to retrieve JSON data from the server, and then convert the JSON string to a JavaScript Object using the JavaScript eval () method, you can use additional JavaScript to extract data from the object without processing the DOM.

Javascript json is often used in projects.

First, let's talk about what a javascript json string is. A json string belongs to an object in javascript and has the corresponding key and value.

The general format is:

a = {a1 : 1,a2 : 'abc',a3 : 'abc',a4 : [1,2,3],a5 : function(){console.log(12)}};

The method for reading this json is to traverse through the for in loop;

The advantage of using a json string is that you can read the data in the database in advance and save the data in the form of json. Then, you can use javascript to read the data in it. This can greatly reduce the number of requests to the server, this increases the loading efficiency of front-end pages.

We will encounter many problems when traversing json. The problem I encountered today is that when we write the key value of the json string as a number, in the traversal of mainstream browsers, the output is not sequential according to the structure of the json output, but in ascending order of numbers. However, browsers below ie8 will output data in the order we output.

For example:

a = {'1' : 1,'5' : function(){console.log(12)},'2' : 'abc','4' : [1,2,3],'3' : {'5' : 'abc', '6' : 'bcd'},};

Loop through for in

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

Browser outputs such as chrome and firefox: 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 solutions to this problem:

First, it is to change the key value of a number to a string containing letters or underscores;

Second: To solve the compatibility of ie, we first traverse json and then store the key value in an array, and then sort the array to loop through the array to retrieve the data in json.

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]]);}

The data read by the browser will be consistent. The disadvantage is that the data will not be output in json format as we output, but it only solves the compatibility problem of the browser.

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.