jquery uses the $.getjson () method to load the JSON file on demand, and the JSON file cannot exist without annotations, or it cannot be traversed.
Traversal JSON array with global function $.each (), of course, native for (xx in XX) can also be used!
The format rules for JSON are simple enough to be made clear with a single page of hundreds of words, and Douglas Crockford claims that the formatting rules never have to be upgraded because the rules are set.
1 The Mapped collection (object) is represented by a curly brace ("{}"): The object is an unordered "' name/value ' Pair" collection. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each mapping "name" followed by a ":" (colon), each mapping value (value) can be a double quotation mark string (string), numeric (number), True, False, NULL, objects (object), or array, These structures can be nested; The "," (comma) is used to separate each map ("' Name/value ' pair").
2 the Set (array) of parallel data is represented by square brackets ("[]"): An array is an ordered set of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. The collection of parallel data is composed of mapped collections (objects), separated by "," (comma) data.
JSON file contents:
The code is as follows |
Copy Code |
[{ "Key1-1": "Value1", "Key1-2": "value2" }, { "key2-1": "Value3", "Key2-2": "Value4", "Key2-3": { "Key2-3-1": "Value5", "Key2-3-2": "Value6" } }, { "Key3-1": "Value7", "Key3-2": { "key3-2-1": "Value8", "Key3-2-2": "Value9" } }] |
Click Shrink code to facilitate browsing other content JQ traversal output:
The code is as follows |
Copy Code |
<script> var temp= ""; $.getjson (' Test.json ', function (JSON, textstatus) {/* parameter JSON refers to the contents of the Test.json file * * * $.each (JSON, function (index, VAL) {/* uses the jquery $.each () to traverse the output JSON file content * * temp+= "<pre>"; $.each (Val, function (i, v) { if (v instanceof Object) { for (I2 in V) { temp+=i2+ ":" +v[i2]+ "<br/>"; } Return } temp+=i+ ":" +v+ "<br/>"; }); temp+= "</pre>"; }); $ (temp). Appendto ($ ("body")); }); </script> |