[英]how to convert a text file into a json object in javascript?
i have a text file that it's structure is like a json object, when i read from file with fs.readFileSync(filePath).toString() command, it converts it to string, then how can i use it as a json object?
您可以使用JSON.parse('json string here')
。
JSON.parse('{ "a": "123" }');
如果文本内容无效 json,如{ a: 123 }
,
function parse(text) {
return Function(`return ${ text }`)();
}
parse('{ a: 123 }');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.