This article mainly introduces the JSON Traversal method, summarizes and analyzes common techniques for json traversal in JavaScript operations based on the instance form, and provides an example summary, for more information about JSON traversal, see the example in this article. We will share this with you for your reference. The details are as follows:
First: Use the for Loop
Js Code:
Function cyclic gjson1 () {var testJson = '[{"name": "Xiaoqiang", "age": 16 },{ "name": "James", "age ": 17}] '; testJson = eval ("(" + testJson + ")"); for (var I = 0; I <testJson. length; I ++) {alert (testJson [I]. name );}}
Type 2: Use the keyword in
Js Code:
Function cyclic gjson2 () {var testJson = '[{"name": "Xiaoqiang", "age": 16 },{ "name": "James", "age ": 17}] '; testJson = eval ("(" + testJson + ")"); for (var I in testJson) {alert (testJson [I]. name );}}
Third, use the jquery. each () function.
Function cyclic gjson3 () {var testJson = '[{"name": "Xiaoqiang", "age": 16 },{ "name": "James", "age ": 17}] '; testJson = eval ("(" + testJson + ")"); $. each (testJson, function (I, n) {alert (I); // I is the index value alert (n. name); // n indicates the traversal value });}
Comprehensive instance: