大家好,我有这个数组
["..\Uploades\Product\1.jpg", "..\Uploades\Product\2.jpg", "..\Uploades\Product\30304_dz6AW8Tp.jpg"]
我想将其转换为这个 JSON
[
{
"image": "..\Uploades\Product\1.jpg",
"thumb": "..\Uploades\Product\1.jpg",
"folder": "Small"
},
{
"image": "..\Uploades\Product\2.jpg",
"thumb": "..\Uploades\Product\2.jpg",
"folder": "Small"
},
{
"image": "..\Uploades\Product\30304_dz6AW8Tp.jpg",
"thumb": "..\Uploades\Product\30304_dz6AW8Tp.jpg",
"folder": "Small"
}
]
我该怎么办?
最佳答案
var arr = ["..\\Uploades\\Product\\1.jpg", "..\\Uploades\\Product\\2.jpg", "..\\Uploades\\Product\\30304_dz6AW8Tp.jpg"] ;
您可以使用map获取对象数组,然后 stringify它获取 JSON:
var JSON = JSON.stringify(arr.map(function(x) {
return {image:x, thumb:x, folder:'Small'};
}));
别忘了shim Array.prototype.map和 JSON.stringify如果您需要支持旧版浏览器。
关于javascript - 如何将文字数组转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21786394/