繁体   English   中英

如何在 javascript 中将文本文件转换为 json object?

[英]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.parse(myString)

文档

您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM