简体   繁体   English

如何将文字数组转换为json

[英]how to convert literal array to json

hello to all i have this array 大家好,我有这个数组

["..\Uploades\Product\1.jpg", "..\Uploades\Product\2.jpg", "..\Uploades\Product\30304_dz6AW8Tp.jpg"] 

i wanna convert it to this JSON 我想把它转换成这个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"
    }
            ]

what should i do? 我该怎么办?

Try this 尝试这个

var arr= ["..\Uploades\Product\1.jpg", "..\Uploades\Product\2.jpg", "..\Uploades\Product\30304_dz6AW8Tp.jpg"];
var json = [];
$.each(arr, function(i,v){
    json.push({"image":v,"thumb":v,"folder":"small"});
});

console.log(json);

FIDDLE 小提琴

var arr = ["..\\Uploades\\Product\\1.jpg", "..\\Uploades\\Product\\2.jpg", "..\\Uploades\\Product\\30304_dz6AW8Tp.jpg"] ;

You can use map to get an array of objects, and then stringify it to get JSON: 您可以使用map来获取对象数组,然后将其字符串化以获取JSON:

var JSON = JSON.stringify(arr.map(function(x) {
    return {image:x, thumb:x, folder:'Small'};
}));

Don't forget to shim Array.prototype.map and JSON.stringify if you need to support older browsers. 如果您需要支持旧浏览器,请不要忘记填充Array.prototype.mapJSON.stringify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM