Javascript JSON.stringify对象,其键在子对象中不带引号
我的当前对象如下所示:Javascript JSON.stringify对象,其键在子对象中不带引号,javascript,json,google-apps-script,google-apps-script-addon,Javascript,Json,Google Apps Script,Google Apps Script Addon,我的当前对象如下所示: var file_data = ({ "file_name_as_key":{ "id":"file_name_as_id", "title":"file title", "type":"extention type", "cat":"category title", "cost":"",
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"
}
});
有人能建议如何进行,以便我可以得到所需格式的对象吗
提前感谢。据我所知,您希望将其保存为:
[{}, {}]
但不是:
({}, {})
因此,创建对象后,将其推送到数组:
file_data.push(fileData)
现在,您可以随时遍历文件数据数组。实际上,我需要的是很容易实现的。我所要做的就是在函数的return语句中添加replace
return JSON.stringify(file_data).replace(/"(\w+)"\s*:/g, '$1:');
您需要的格式不是JSON。JSON要求在属性名周围加引号。所需的非JSON语法的实际目标是什么?您认为您发布的两种格式之间的区别是什么?我可以知道您为什么要这样做吗?这没有任何区别。原因是,在以后的阶段使用$对每个元素进行grub比较容易。我想每个元素都是?@AlexB-no;在JS对象中,您指定了带引号或不带引号的键,这两个对象之间的实现是零差异的。除了Pointy指出的以外,呃,后者是无效的JSON.the.push在Code.gs中不起作用,因为fileData是一个数组[[],[],[],[]fileData应该是对象而不是数组。
file_data.push(fileData)
return JSON.stringify(file_data).replace(/"(\w+)"\s*:/g, '$1:');