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.