This article introduces the usage of getJSON () in jquery-json file content traversal and output methods. For more information, see.
In jquery, you can use the $. getJSON () method to load json files as needed. json files cannot be annotated, or they cannot be traversed.
Use the global function $. each () to traverse the json array. Of course, the native for (xx in xx) can also be used!
The Json format rule is very simple. It can be clearly stated with only a few hundred words on a page, and Douglas Crockford claims that this format rule never needs to be upgraded because it is stipulated in all the provisions.
1) The ing set (object) is represented by braces ("{}"): This object is an unordered set of "'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each ing "name" is followed by a ":" (colon); each ing value (value) can be a string enclosed in double quotation marks, a value (number), true, false, null, object, or array. These structures can be nested. ", separated by commas.
2) The set (array) of the parallel data is represented by square brackets ("[]"): An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. A set of parallel data is composed of a ing set (object). The parallel data is separated by commas.
Json file content:
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 ", "The key2-3-2": "value6" } },{ "Key3-1": "value7 ", "Key3-2 ":{ "Key3-2-1": "value8 ", "Key3-2-2": "value9" } }] |
Click contract code to view other content. jq traverses the output:
The Code is as follows: |
Copy code |
<Script> Var temp = ""; $. GetJSON ('test. json', function (json, textStatus) {/* The json parameter indicates the content of the test. json file */ $. Each (json, function (index, val) {/* use jquery $. each () to traverse and 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>
|