【发布时间】:2018-04-02 22:16:39
【问题描述】:
这是我下面的 Javascript 代码示例。我写这篇文章的主要原因是我有一个关于最后一个函数的问题,即我的 GalleryImage 函数。该函数中有四个字符串变量。在每个变量中,我应该将对应键中的一个元素存储在我的 JSON 文件的数组中。我怎么做?我的 javascript 代码下面是我的 JSON 文件代码示例。
function GalleryImage(mJSON) {
var locate = "";
var description = "";
var date = "";
var url = "";
//implement me as an object to hold the following data about an image:
//1. location where photo was taken
//2. description of photo
//3. the date when the photo was taken
//4. either a String (src URL) or an an HTMLImageObject (bitmap of the photo. https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement)
}
"images": [
{
"imgPath": "img/places/australia.jpg",
"imgLocation": "Australia",
"description": "Loch Ard Gorge",
"date": "01/01/2016"
},
{
"imgPath": "img/places/austria.jpg",
"imgLocation": "Austria",
"description": "Austrian chapel",
"date": "01/02/2016"
}
]
}
【问题讨论】:
-
@Biffen 我编辑了它
-
JSON 是一种符号格式。如果您执行
var images = [,您将拥有一个 JS 对象。我想这就是你真正的意思。请创建minimal reproducible example 解释输入和预期输出 -
@mplungjan 从那个 var images 代码开始是我的 JSON 文件,所以它是 JSON 代码。我只需要了解如何将图像数组中的每个键放入我编写的 Javascript 函数中的变量中。变量将是字符串。
-
作为“JSON 码”没用 你会得到一个 JS 对象或者你需要使用 JSON.parse 使字符串成为一个 JS 对象。
标签: javascript html css json