* 更新:* 我也将changeSet 用双引号引起来,尽管我仍然遇到相同的错误
这就是我想要实现的目标
正在调用脚本函数,该函数检查项目中新修改的文件,并将这些文件作为 JSON 形式的字符串作为参数传递给另一个 Node 程序
我可以在哪里使用该 Json 来执行任务
但是我无法将该字符串解析为 JSON
所以有问题的 JSON 是这样的 {"changeSet":[{"time":"2019-06-2810:22:57","fileName":"src/MainHandler.jsx"},{"time":"2019-06-2013:08:45","fileName":"resSet.json"}]}
Node findChangeschangesJson
在将 this 作为参数传递的函数中
我尝试了各种各样的事情
就像通过JSON.parse方法解析它
trim 参数然后解析它,无论我做什么,我都会不断收到语法错误
我也尝试过将changeSet属性括在双引号中,但我仍然遇到相同的错误
let cs = process.argv[2];
let b = JSON.parse(cs); //here I get the parsing error
console.log(b.changeSet);
//i have also tried stringifying it first and parsing it
//trimming it --->stringifying it ---->parsing it
undefined:1
{changeSet
^
SyntaxError: Unexpected token c in JSON at position 1
at JSON.parse (<anonymous>)
最佳答案
有问题的 JSON 无效,您需要在changeSet 周围添加引号,例如
{
"changeSet": [
{
"time": "2019-06-2810:22:57",
"fileName": "src/MainHandler.jsx"
},
{
"time": "2019-06-2013:08:45",
"fileName": "resSet.json"
}
]
}
关于javascript - 无法解析从其他 Node 程序作为参数传递的 json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56802614/