Getjson () Usage-json file content traversal output method in jquery

Source: Internet
Author: User

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>

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.