JSON traversal method instance summary _ javascript skills

Source: Internet
Author: User
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:

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.