我有字符串数据
var str2json = require('string-to-json');
var data={
"GTIN" : "GTIN 3",
"Target Market" : "Target Market 3",
"Global Location Provider Name(GLN) 3" : "Global Location Provider Name(GLN) 3",
"Information Provider Name 3" : "Information Provider Name 3",
"Product Overview" : "Product Overview 3",
}
我正在使用str2json模块我将其转换为JSON对象,
当我使用var output = str2json.convert(data);
时它对我不起作用,但在我使用时
var output = str2json.convert({
"GTIN" : "GTIN 3",
"Target Market" : "Target Market 3",
"Global Location Provider Name(GLN) 3" : "Global Location Provider Name(GLN) 3",
"Information Provider Name 3" : "Information Provider Name 3",
"Product Overview" : "Product Overview 3",
})
它工作正常并且像这样给出了输出
{ GTIN: 'GTIN 3',
'Target Market': 'Target Market 3',
'Global Location Provider Name(GLN) 3': 'Global Location Provider Name(GLN) 3',
'Information Provider Name 3': 'Information Provider Name 3',
'Product Overview': 'Product Overview 3' }
那么var output = str2json.convert(data);
答案 0 :(得分:0)
对于字符串到json的转换,您可以使用builtin JSON.parse()参考:JSON -Documentation
代码中数据对象的更多内容是有效的JSON对象(如果删除尾随逗号)。
{
"GTIN": "GTIN 3",
"Target Market": "Target Market 3",
"Global Location Provider Name(GLN) 3": "Global Location Provider Name(GLN) 3",
"Information Provider Name 3": "Information Provider Name 3",
"Product Overview": "Product Overview 3"
}