Simple Example of json format data parsing using JavaScript, javascriptjson
The following json data is used to store the pre-loaded image path:
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe Code is as follows:
Var g = getData ("p1 ");
Console. log (g );
The output result is: images/p1.jpg.