Simple Example of parsing json format data using JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces a simple example of JavaScript parsing json format data. This article uses the for loop to obtain json node data, you can refer to the following json data string to store the pre-loaded image path:

The Code is as follows:


Var imgData = [
{Name: "p1", src: "images/p1.jpg "},
{Name: "p2", src: "images/p2.jpg "},
{Name: "p3", src: "images/p3.jpg "},
{Name: "p4", src: "images/p4.jpg "},
{Name: "p5", src: "images/p5.jpg "}
]

The following function obtains the path src of the row through the name of each line in json. Let's take a look at the Code:

The Code is as follows:


Function getData (name ){
Var picArr = imgData;
Var picSrc;
For (var I = 0; I <picArr. length; I ++ ){
Var cur_person = picArr [I];
If (cur_person.name = name ){
PicSrc = cur_person.src;
}
}
Return picSrc;
}

After the function is executed, the src of the row is returned.

The Code is as follows:


Var g = getData ("p1 ");
Console. log (g );

The output result is: images/p1.jpg.

Related Article

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.