gpt4 book ai didi

javascript - Node.js 中的有效 JSON 发生位置 0 处 JSON 中的意外标记

转载 作者:行者123 更新时间:2023-12-01 01:47:20 25 4
gpt4 key购买 nike

我有一个名为 HeartbeatResponse_v1p0.json 的文件,其内容如下:

{
"$schema": "http://json-schema.org/draft-06/schema#",
"$id": "urn:OCPP:Cp:2:2018:4:HeartbeatResponse",
"comment": "OCPP 2.0 - v1p0",
"type": "object",
"additionalProperties": true,
"properties": {
"currentTime": {
"type": "string",
"format": "date-time"
}
},
"required": [
"currentTime"
]
}

现在在 index.js 中,我正在尝试 JSON.parse 它。

const fs = require('fs')
var schema = fs.readFileSync('./HeartbeatResponse_v1p0.json')
console.log(JSON.parse(schema.toString()))

但我收到以下错误:

undefined:1
{
^
SyntaxError: Unexpected token in JSON at position 0
at JSON.parse (<anonymous>)
.........

我不知道为什么会发生这种情况。请帮助我。

最佳答案

替换 JSON.parse(schema.toString())JSON.parse(schema.toString().trim())成功了!

关于javascript - Node.js 中的有效 JSON 发生位置 0 处 JSON 中的意外标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51853247/

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