We often use javascript Of json.
First of all javascript Of json What is string ,json The string belongs to javascript An object of , Objects with keys and values .
The general format is :
a = {
a1 : 1,
a2 : 'abc',
a3 : 'abc',
a4 : [1,2,3],
a5 : function(){console.log(12)}
};
We read this json The way is through for in Loop to traverse ;
Use json The advantage of string is that the data in the database can be read out and saved in one time in advance json In the form of , And then through javascript To read what belongs to , This can greatly reduce the number of requests to the server , Increase the loading efficiency of the front page .
We're traversing json There are a lot of problems when I'm going to work , The problem I have today is that we put json When the key value of a string is written as a number , In the mainstream browser traversal time will not follow our output json The structure of the output , It's output in the order of numbers from small to large . however ie8 The following browsers will output in the order we output .
for example :
adopt for in loop
chrome,firefox Wait for the 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 :
First of all : It is to change the key value of a number into a string, including letters or underscores ;
second : That's the solution ie The compatibility of , By traversing first json Then put the key values in an array , And then through the array sorting to loop the array to get json Data in .
Specific code :
var arr = [],sortNumber = function (a,b){
arr.sort(sortNumber);
for(var x = 0; x < arr.length; x++) {
console.log(a[arr[x]]);
}
In this way, the data read by the browser will be consistent , One drawback is that this will not be exported according to our output json Format output , It just solves the problem of browser compatibility .
About the traverse javascript Medium json String browser output results are not uniform situation more related articles
- Java and JavaScript Use in Json Method Daquan
Lin Bingwen Evankaka Original works . Reprint please indicate the source http://blog.csdn.net/evankaka Abstract :JSON(JavaScript Object Notation) It's a lightweight number ...
- JavaScript Use in JSON, namely JS operation JSON summary
JSON(JavaScript Object Notation Object tag ) Is a lightweight data exchange format , Use a completely language independent text format , Is the ideal data exchange format . meanwhile ,JSON yes JavaScript Native ...
- Json Learning summary (1)——Java and JavaScript Use in Json Method Daquan
Abstract :JSON(JavaScript Object Notation) Is a lightweight data exchange format . It's based on ECMAScript A subset of . JSON Use a completely language independent text format , But it also uses something like C language ...
- JavaScript Chinese analysis JSON --- json.js 、 json2.js as well as json3.js Use difference of
JSON official (http://www.json.org/) Provides a json.js,json.js yes JSON The official offer is in JavaScript Chinese analysis JSON Of js package ,json.js.json2.js.js ...
- Easy to use JSON,JavaScript Created in JSON object ( One )
JSON:JavaScript Object notation (JavaScript Object Notation). JSON Is the syntax for storing and exchanging text information . similar XML. JSON Than XML smaller . faster , Easier to parse . ...
- stay javascript Use in Json
jSON yes JavaScript A subset of object-oriented Syntax . because JSON yes JavaScript A subset of , So it can be clearly used in this language . The text generated json object , You have to put a pair of parentheses on the outside . js Code var m ...
- Javascript Use in Json There are four ways to do it
1.jQuery The conversion mode supported by the plug-in : Copy the code as follows : $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr), Can be json The string is converted to json object ...
- stay JavaScript Use in json.js:Ajax Project GET request ( Sync )
1. use php Write a response program that provides data (phpdata.php) <?php $arr=array(1,2,3,4); // Encode the array as JSON Formatted data $jsonarr=json_encod ...
- Python Read... From the file in json strand , And write it to Excel In the table
Json:JavaScript Objective Notation, Is a lightweight data exchange format .Json The most widely used is as AJAX in web Data format of communication between server and client . It's also commonly used now http In request , ...
Random recommendation
- Java Connect remote Redis
redis-server & // Background start redis redis-cli // Use redis open redis.conf The file in NETWORK Part of it says /usr/local/src ...
- mybatis Simple comparison between single insert and batch insert
stay J2EE In the project ,mybatis As the mainstream persistence layer framework , A lot of knowledge is worth studying , today , Record the data insertion performance ( Single insert and batch insert ). One , Test object public class Test { private ...
- js Array traversal for..in disadvantages
//for..in Disadvantages in arrays In principle, arrays Array Objects cannot be manipulated , But some programmers don't pay attention to Array If you add a method to the prototype chain of, unexpected bug // for example ,,]; Array.prototy ...
- BZOJ-3576 Jiangnan music game + Optimize
fye Test the original question , Senior one kneels down , In addition to CA It seems that all of you A spicy ( flee ) 3576: [Hnoi2014] Jiangnan music Time Limit: 30 Sec Memory Limit: 512 MB Submit: 1 ...
- Shortcut key for CodeBlocks
One .not only in CodeBlocks 13.12 Undo last action Ctrl + Z // back off Redo last action Ctrl + Shift + Z // Forward ...
- HTTP Basics 03--HTTP message
The general structure of the message : Message header : Request line ( Request message )/ Status line ( response message ) Header field : Various headers of various conditions and attributes of requests and responses : Other (Cookie) Blank line Message body ( It's not necessary ) Coding increases transmission rate : Message body and real ...
- Take out a int Every one of them , Use algorithms
int a=1234: int current: while(a) { current=a%10://4 cout<<current; a=a%10; }
- SQL Server Database learning notes -E-R Model
Entity (Entities) contact (Relationships) Model abbreviation E-R The model is also called E-R Method , By P.P.Chen On 1976 First proposed in . There is also a key element Attributes- attribute , It provides data that is not subject to any database ...
- Spring : Conquer database ( two )
This section describes Spring and ORM Integration Framework . Even though Hibernate Open source ORM The community is very popular . however , This article will MyBatis Case explanation . also MyBatis and Hibernate Good or bad is meaningless , It mainly depends on the actual demand , have interest in ...
- makefile Document knowledge point record
1 assignment = Is the most basic assignment := Is the value before the override ?= If it's not assigned += Is to add the value after the equal sign 1.“=” make Will bring the whole makefile After deployment , And then determine the value of the variable . in other words , The value of the variable will be the entire make ...