JSON.stringify 带有键的对象在子对象中没有引号

JSON.stringify object with keys without the quotes in subobject

我当前的对象是这样的:

var file_data = ({
            "file_name_as_key":{
                "id":"file_name_as_id",
                "title":"file title",
                "type":"extention type",
                "cat":"category title",
                "cost":"",
                "desc":"Some text that will go here \"Something in quotes\" <strong>Something as bold</strong> some more text.",
                "img":"image url",
                "url":"Coresponding page url ",
                "status":"Updated"
            }
        });

我需要的是:

var file_data = ({
            "file_name_as_key":{
                id:"file_name_as_id",
                title:"file title",
                type:"extention type",
                cat:"category title",
                cost:0,
                desc:"Some text that will go here \"Something in quotes\" <strong>Something as bold</strong> some more text.",
                img:"image url",
                url:"Coresponding page url ",
                status:"Updated"
            }
        });

我在 Code.gs 文件中创建对象,然后将对象作为 JSON.stringify(my_obj).

传递给模板

这是我的脚本:

    var file_data = {};

var i = 0;
fileData.forEach(function (row) {
  i++;
  if(i >= 2){
    file_data[row[8]] = {
        id: row[8],
        title: row[0],
        type: row[1],
        cat: row[2],
        cost: row[3],
        desc: row[4],
        img: row[5],
        url: row[6],
        status: row[7]
    }
  }
});

return JSON.stringify(file_data);

任何人都可以建议如何去做,以便我可以获得所需格式的对象吗?

提前致谢。

据我了解,您想将其保存为:

[{}, {}]

但不是:

({}, {})

因此,在创建对象后,将它们推送到一个数组中:

file_data.push(fileData)

现在,您可以随时遍历 file_data 数组。

其实我想要的很容易实现。我所要做的就是将 replace 添加到函数

中的 return 语句中
return JSON.stringify(file_data).replace(/"(\w+)"\s*:/g, ':');