May 08, 2021 JSON
This article obtains jason node data through the for loop, and the required friends can refer to the following string of jason data to store preloaded picture paths:
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"
}
]
Here's a function that gets the path src for each line of jason, so let's look at the code:
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;
}
The src of the row is returned after the function is executed.
var g = getData("p1");
console.log(g);
Once output, you can see that the result is: images/p1 .jpg
The mutual conversion of JS object and JSON format data is actually two problems: JS object is converted to JSON format data, JSON format data is converted to JS object. /b20> The current project data interactions are almost always with JQuery, so the process is: front-end page data -JS object-jQuery commit-python processing, the other is upside down. P ython certainly can't process JS object data directly, so convert JS objects into a data format that python can handle (usually dictionary diction), just as python takes data back to the front end and converts dictionary data into objects that JS can process, which is usually JSON.