Javascript 导入JSON后从特定值中删除双引号
我正在导入一个外部存储的JSON文件数组。在JSON中,有一个键值对“dataType”,其中值存储为字符串。对于第三方应用程序,应将具有某些值的数组作为不带双引号的属性 当前状态:Javascript 导入JSON后从特定值中删除双引号,javascript,arrays,Javascript,Arrays,我正在导入一个外部存储的JSON文件数组。在JSON中,有一个键值对“dataType”,其中值存储为字符串。对于第三方应用程序,应将具有某些值的数组作为不带双引号的属性 当前状态: ...{id: "label", dataType: "tableau.dataTypeEnum.string"}... ...{id: "label", dataType: tableau.dataTypeEnum.string}... 预期格式:
...{id: "label", dataType: "tableau.dataTypeEnum.string"}...
...{id: "label", dataType: tableau.dataTypeEnum.string}...
预期格式:
...{id: "label", dataType: "tableau.dataTypeEnum.string"}...
...{id: "label", dataType: tableau.dataTypeEnum.string}...
我试过这样做,但双引号仍然存在。谢谢你的提示
脚本:
...
schemaFromFile = JSON.parse(getRemote());
/*
* Convert dataType to property to avoid dbl quoted values
*/
Object.keys(schemaFromFile).forEach(function(key) {
if(schemaFromFile[key].hasOwnProperty('dataType'))
schemaFromFile[key].resolve = eval(schemaFromFile[key].resolve);
});
console.log('schema', schema)
function getRemote() {
return $.ajax({
type: "GET",
url: schemaURL,
async: false
}).responseText;
};
...
请在中添加一个示例以及结果输出,这样我们就可以使用了。我会认真重新考虑您的方法,因为它需要使用
eval
(这是众所周知的不安全)。类似于映射到适当名称的一组已知类型的方法会更安全、更易于调试。@DavidCulbreth有一个好主意:为数据类型创建一个对象,它具有字符串格式的键tableau.dataTypeEnum.string
,变量本身为tableau.dataTypeEnum.string
。然后只需调用该对象/键即可获得适当的值。@Rojo是的,但OP want是存储在schemaFromFile[key]
中的元素,而不是键(属性名称)schemaFromFile类似于{file1:{label:…,dataType:…},“file2:{label:…,dataType:…}
和键将是“file1”
,“file2”
,…请在中添加一个示例以及结果输出,以便我们可以使用。我将认真重新考虑您的方法,因为它需要使用eval
(这是众所周知的不安全)。类似于映射到适当名称的一组已知类型的方法会更安全、更易于调试。@DavidCulbreth有一个好主意:为数据类型创建一个对象,它具有字符串格式的键tableau.dataTypeEnum.string
,变量本身为tableau.dataTypeEnum.string
。然后只需调用该对象/键即可获得适当的值。@Rojo是的,但OP want是存储在schemaFromFile[key]
中的元素,而不是键(属性名称)
schemaFromFile类似于{file1:{label:…,dataType:…},“file2:{label:…,dataType:…}
和键将是“file1”
,“file2”
。。。