【发布时间】:2020-08-07 08:36:49
【问题描述】:
我有 json 文件,但是 require("./screens/test/test.jsx") 附近的 json 文件格式错误,我需要通过 javascript 修复这个错误的 json 格式
我尝试了一个 json 格式化程序网站,该网站正在修复我错误的 json 格式。但我无法从 javascript 修复这种 json 格式
你可以通过这个https://jsonformatter.curiousconcept.com/修复这个错误的json
[
{
"screenName": "screen1",
"path": "path1",
"componentPath": require('./screens/test/test.jsx')
},
{
"screenName": "screen1",
"path": "path1",
"componentPath": require('./screens/test/test.jsx')
}
]
从上面应该得到的是:
[
{
"screenName": "screen1",
"path": "path1",
"componentPath": "require('./screens/test/test.jsx')"
},
{
"screenName": "screen1",
"path": "path1",
"componentPath": "require('./screens/test/test.jsx')"
}
]
【问题讨论】:
-
我不明白。您是要修复错误的 json,还是编写一个修复错误 json 的 javascript 函数?
-
@DevOfZot 我想写个js函数来修复这种类型的json
-
这或多或少相当于写一个 JSON 解析器,相当复杂。您最好的选择是在 try-catch 中执行“JSON.parse(string)”,如果它发现错误,请使用正则表达式修复您之前看到的已知错误。
标签: javascript node.js json regex