Before you introduce the text, let's say what JSON is.
JSON (JavaScript Object notation) is a lightweight data interchange format that we call JavaScript Object notation. One of the advantages of using JSON for data transfer is that JSON is actually JavaScript. It is based on a text format for a subset of JavaScript object literal syntax in the ECMAScript 3rd edition. This means that you can use ResponseText to retrieve JSON data from the server, and then use the JavaScript eval () method to convert the JSON string into a JavaScript object. With additional JavaScript, you can extract data from this object very well without having to process the DOM.
We often use JavaScript json when we do the project.
First, let's say what the JavaScript JSON string is, and the JSON string belongs to one of the JavaScript objects, with keys and values corresponding to the object.
The General format is:
A = {
a1:1,
A2: ' abc ',
A3: ' abc ',
A4: [1,2,3],
a5:function () {Console.log ()}
;
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 in the form of JSON in advance, and then read the inside by JavaScript, which can greatly reduce the number of requests to the server and increase the loading efficiency of the front page.
We have a lot of problems traversing JSON, and the problem I'm having today is that when we write the key values of the JSON string in numbers, we don't output the JSON in our main browser as we output it, but we output it from small to large numbers. But IE8 the following browsers will output in the order we output them.
For example:
A = {
' 1 ': 1,
' 5 ': function () {Console.log ()},
' 2 ': ' abc ',
' 4 ': [1,2,3],
' 3 ': {' 5 ': ' abc ', ' 6 ' : ' BCD '},
};
Loop through in
for (var i in a) {
console.log (a[i]);
}
Chrome,firefox such as 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 such problems:
The first is to change the key value of a number to a string, including letters or underscores;
Second: To solve the compatibility of IE, by first traversing the JSON and then the key value into an array, and then through the array ordering to loop the array of 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 + +) {
console.log (a[arr[x]]);
The data that the browser reads will remain the same, and there is a drawback that this will not output in the JSON format we output, just to solve the browser compatibility problem.
The above is a small set of JavaScript for you to walk through the JSON browser output of the results of the problem is not uniform, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!