简体   繁体   English

JSON.parse() 导致错误:`SyntaxError: Unexpected token in JSON at position 0`

[英]JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0`

I'm writing my first application in Node.js.我正在用 Node.js 编写我的第一个应用程序。 I am trying to read some data from a file where the data is stored in the JSON format.我正在尝试从数据以 JSON 格式存储的文件中读取一些数据。

I get this error:我收到此错误:

SyntaxError: Unexpected token  in JSON at position 0 SyntaxError: Unexpected token in JSON at position 0

at Object.parse (native)在 Object.parse(本机)

Here is this part of the code:这是代码的这一部分:

//read saved addresses of all users from a JSON file
fs.readFile('addresses.json', function (err, data) {
    if (data) {
        console.log("Read JSON file: " + data);
        storage = JSON.parse(data);

Here is the console.log output (and I checked the .json file itself, it's the same):这是console.log输出(我检查了 .json 文件本身,它是一样的):

Read JSON file: {

    "addresses": []

}

That seems to me like a correct JSON.在我看来,这就像一个正确的 JSON。 Why does JSON.parse() fail then?为什么JSON.parse()会失败?

You have a strange char at the beginning of the file.文件开头有一个奇怪的字符。

data.charCodeAt(0) === 65279

I would recommend:我会推荐:

fs.readFile('addresses.json', function (err, data) {
if (data) {
    console.log("Read JSON file: " + data);
    data = data.trim(); 
    //or data = JSON.parse(JSON.stringify(data.trim()));
    storage = JSON.parse(data);
 }});

JSON.parse() does not allow trailing commas. JSON.parse()不允许尾随逗号。 So, you need to get rid of it:所以,你需要摆脱它:

JSON.parse(JSON.stringify(data));

You can find more about it here .您可以在此处找到更多相关信息。

It might be the BOM[ 1 ].它可能是 BOM[ 1 ]。 I have done a test by saving a file with content {"name":"test"} with UTF-8 + BOM, and it generated the same error.我通过使用 UTF-8 + BOM 保存内容为{"name":"test"}的文件进行了测试,它生成了相同的错误。

> JSON.parse(fs.readFileSync("a.json"))
SyntaxError: Unexpected token  in JSON at position 0

And based on a suggestion here [ 2 ], you can replace it or drop it before you call JSON.parse() .根据此处 [ 2 ] 的建议,您可以在调用JSON.parse()之前替换或删除它。

For example:例如:

var storage = {};

fs.readFile('a.json', 'utf8', function (err, data) {
    if (data) {
        console.log("Read JSON file: " + data);
        console.log(typeof(data))
        storage = JSON.parse(data.trim());
    }
});

or

var storage = {};
fs.readFile('a.json', function (err, data) {
    if (data) {
        console.log("Read JSON file: " + data);
        console.log(typeof(data))
        storage = JSON.parse(data.toString().trim());
    }
})

You can also remove the first 3 bytes (for UTF-8) by using Buffer.slice() .您还可以使用Buffer.slice()删除前 3 个字节(对于 UTF-8 Buffer.slice()

try it like this像这样试试

fs.readFile('addresses.json','utf-8', function (err, data) {
    if (data) {
        console.log("Read JSON file: " + data);
        storage = JSON.parse(data);

its because of the BOM that needs an encoding to be set before reading the file.这是因为在读取文件之前需要设置编码的 BOM。 its been issued in nodejs respository in github它已在 github 的 nodejs 存储库中发布

https://github.com/nodejs/node-v0.x-archive/issues/186 https://github.com/nodejs/node-v0.x-archive/issues/186

To further explain @Luillyfe's answer:进一步解释@Luillyfe的回答:

Ah-ha!啊哈! fs.readFileSync("data.json") returns a Javascript object! fs.readFileSync("data.json")返回一个 Javascript 对象!

Edit: Below is incorrect...But summarizes what one might think at first!编辑:下面是不正确的......但总结了人们一开始可能会想到的!

I had through that was a string.我经历过那是一个字符串。 So if the file was saved as UTF-8/ascii, it would probably not have an issue?那么如果文件被保存为 UTF-8/ascii,它可能不会有问题? The javascript object returned from readFileSync would convert to a string JSON.parse can parse?从 readFileSync 返回的 javascript 对象将转换为字符串 JSON.parse 可以解析? No need to call JSON.stringify?不需要调用 JSON.stringify?

I am using powershell to save the file.我正在使用 powershell 来保存文件。 Which seems to save the file as UTF-16 (too busy right now to check).这似乎将文件保存为 UTF-16(现在太忙无法检查)。 So I get "SyntaxError: Unexpected token in JSON at position 0."所以我得到“SyntaxError: Unexpected token in JSON at position 0.”

However, JSON.stringify(fs.readFileSync("data.json")) correctly parses that returned file object into a string JSON can parse.但是, JSON.stringify(fs.readFileSync("data.json"))正确地将返回的文件对象解析为 JSON 可以解析的字符串。

Clue for me is my json file contents looking like the below (after logging it to the console):我的线索是我的 json 文件内容如下所示(将其登录到控制台后):

�{ " R o o m I D _ L o o k u p " :   [
         {
                 " I D " :     1 0 ,
                 " L o c a t i o n " :     " f r o n t " ,
                 " H o u s e " :     " f r o n t r o o m "
         }
}

That doesn't seem like something a file would load into a string...这似乎不像文件会加载到字符串中的东西......

Incorrect being (this does not crash...but instead converts the json file to jibberish!):不正确(这不会崩溃……而是将 json 文件转换为乱码!):

const jsonFileContents = JSON.parse(JSON.stringify(fs.readFileSync("data.json")));

I can't seem to find this anywhere.我似乎无法在任何地方找到这个。 But makes sense!不过有道理!

Edit: Um... That object is just a buffer.编辑:嗯...那个对象只是一个缓冲区。 Apologies for the above!对以上内容表示歉意!

Solution:解决方法:

const fs = require("fs");

function GetFileEncodingHeader(filePath) {
    const readStream = fs.openSync(filePath, 'r');
    const bufferSize = 2;
    const buffer = new Buffer(bufferSize);
    let readBytes = 0;

    if (readBytes = fs.readSync(readStream, buffer, 0, bufferSize, 0)) {
        return buffer.slice(0, readBytes).toString("hex");
    }   

    return "";
}

function ReadFileSyncUtf8(filePath) {
    const fileEncoding = GetFileEncodingHeader(filePath);
    let content = null;

    if (fileEncoding === "fffe" || fileEncoding === "utf16le") {
        content = fs.readFileSync(filePath, "ucs2"); // utf-16 Little Endian
    } else if (fileEncoding === "feff" || fileEncoding === "utf16be") {
        content = fs.readFileSync(filePath, "uts2").swap16(); // utf-16 Big Endian
    } else {
        content = fs.readFileSync(filePath, "utf8");
    }

    // trim removes the header...but there may be a better way!
    return content.toString("utf8").trimStart();
}

function GetJson(filePath) {
    const jsonContents = ReadFileSyncUtf8(filePath);
    console.log(GetFileEncodingHeader(filePath));

    return JSON.parse(jsonContents);
}

Usage:用法:

GetJson("data.json");

Note: I don't currently have a need for this to be async yet.注意:我目前还不需要它是异步的。 Add another answer if you can make this async!如果您可以使此异步,请添加另一个答案!

As mentioned by TamusJRoyce I ended up using the util.TextDecoder class to come up with a robust way to read both UTF-8 (without BOM) and UTF-8 (with BOM).正如 TamusJRoyce 所提到的,我最终使用 util.TextDecoder 类提出了一种强大的方法来读取 UTF-8(无 BOM)和 UTF-8(有 BOM)。 Here's the snippit, assuming that file input.json is UTF-8 (with or without BOM) and contains valid JSON.这是代码片段,假设文件 input.json 是 UTF-8(带或不带 BOM)并且包含有效的 JSON。

const fs = require('fs');
const util = require('util');

const rawdata = fs.readFileSync('input.json');
const textDecoder = new util.TextDecoder('utf-8');
const stringData = textDecoder.decode(rawdata);
const objects = JSON.parse(stringData);
const fs = require('fs');
const myConsole = new console.Console(fs.createWriteStream('./output.json'));
myConsole.log(object);

This will create an output file with all the output which can been triggered through console.log(object) .这将创建一个输出文件,其中包含可以通过console.log(object)触发的所有输出。

This is the easiest way to convert the console.log() output into a file.`这是将console.log()输出转换为文件的最简单方法。`

暂无
暂无

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

相关问题 JSON.parse() 错误 SyntaxError: Unexpected token &lt; in JSON at position 0 - JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) JSON.parse(): SyntaxError: Unexpected token in JSON at position 0 - JSON.parse(): SyntaxError: Unexpected token � in JSON at position 0 语法错误:JSON 中的意外令牌 e 在 position 1 与 JSON.parse() - SyntaxError: Unexpected token e in JSON at position 1 with JSON.parse() SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 语法错误:JSON.parse 中位置 0 处的 JSON 中的意外标记 - SyntaxError: Unexpected token " in JSON at position 0 at JSON.parse Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) 语法错误:在 JSON 中的意外令牌 u 在 position 0 在 JSON.parse - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM