gpt4 book ai didi

javascript - 在 JavaScript 中使用 JSON 文件

转载 作者:行者123 更新时间:2023-11-28 16:31:31 25 4
gpt4 key购买 nike

我希望在 Node.js 项目中使用 JSON 文件,但它似乎不起作用 -

var JsonPath = '../../folderOfjsonFiles';
var JsonFile = JsonPath + 'test.json';

var parseThis = JSON.parse(JsonFile);
console.dir(parseThis);

对于我做错了什么有什么建议吗?运行此命令会产生此错误:

    "test1": {
^
uncaught: SyntaxError: Unexpected token :
at Module._compile (module.js:399:25)
at Object..js (module.js:410:10)
at Module.load (module.js:336:31)
at Function._load (module.js:297:12)
at require (module.js:348:19)

其中 test1 是我文件中的第一个 JSON 对象。

这是我的 JSON 文件 -

{
"test1": {
"testname": "alpha",
"password": "password"
}
}

即使在 JSON 解析之前,如何从服务器端本地存储的文件中读取内容?我觉得我把这件事搞得太复杂了。

最佳答案

JSON 对象必须包含在顶层的 {}[] 中,所以你不能这样做

"test1": {...},
"test2": {...}

使用

{
"test1": {...},
"test2": {...}
}

相反。

关于javascript - 在 JavaScript 中使用 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5759078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com