各位大大好!Json檔案內容要如何放圖片路徑呢?我想做按一下nav選單後,用fetch()讀Json檔案,Json檔案內容是名字:照片路徑,一個div顯示名字,另一個div顯示圖片。
假設json長這樣
json
{
"name": "John",
"imagePath": "images/john.jpg"
}
那就
fetch('你的json')
.then(response => response.json())
.then(data => {
let name = data.name;
let imagePath = data.imagePath;
document.getElementById('nameDiv').innerHTML = name;
document.getElementById('imageDiv').innerHTML = `<img src="${imagePath}" alt="${name}">`;
})
.catch(error => console.error(error));
Read the json content from file .
Deserialize the content into object .
Add a new property named image in model and assign the value to it .
Serialize the object into json string .
Write the string into file .