简体   繁体   English

'JSON 输入意外结束'

[英]'Unexpected end of JSON input'

Yesterday it was working perfectly fine and I wasn't getting this error.昨天它工作得很好,我没有收到这个错误。

Tried replacing a bunch of stuff with this one single line and I'm just flat out confused on why this is occurring.尝试用这一行替换一堆东西,我只是对为什么会发生这种情况感到困惑。 I've been researching this for an hour and all the results for 'Unexpected end of JSON input' don't really fully match what I'm doing and are incredibly confusing to me as I am fairly new to javascript.我已经研究了一个小时,“JSON 输入的意外结束”的所有结果并不完全符合我正在做的事情,并且对我来说非常困惑,因为我对 javascript 还很陌生。

Line that's causing it:导致它的行:

let userData = JSON.parse(fs.readFileSync(__dirname + '/Storage/userData.json', 'utf8'))

I should also point out that above the error it says what is causing it is 'undefined:1'???我还应该指出,在错误上方,它说明导致它的原因是“未定义:1”??? So that's pretty weird.所以这很奇怪。 Normally it would show like the line in the javascript code but no, I had to search through the list of "at (something in some script)" to find what line was causing it, I have a feeling it might have to do with the JSON itself instead of my code.通常它会显示为 javascript 代码中的行,但不,我必须搜索“at(某些脚本中的某些内容)”列表才能找到导致它的行,我觉得它可能与JSON 本身而不是我的代码。

The error is from the ./Storage/userData.json file.错误来自 ./Storage/userData.json 文件。 Do you have an empty file?你有空文件吗? If yes, make sure to save it with [] .如果是,请确保使用[]保存它。 Sometimes, the file can be cleared by a code error who affects the data.有时,文件可能会被影响数据的代码错误清除。

This happens when your .json file is empty.当您的 .json 文件为空时会发生这种情况。 This is why I do not recomment sorting data in a .json file, if you edit it alot, it can sometimes be completely wiped out.这就是为什么我不建议对 .json 文件中的数据进行排序的原因,如果您对其进行大量编辑,有时可能会将其完全清除。 To fix it, simply put {} in the file and the error should be gone.要修复它,只需将{}放入文件中,错误就会消失。 Your data will be gone, too though.不过,您的数据也会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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