GetJSON () usage in jquery-json file content traversal Output Method

Source: Internet
Author: User

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>

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.