简体   繁体   中英

JSON file unexpected token at position 0

I am unable to parse my 2 json files for some reason. I get a unexpected token at position 0 for the first file and around position 500 for the second file.Below are the two files

file 1:
{"user0":{"username":"user0","password":"user0","gameHistory":[0], "friends":[], "isPublic":true, "profilePic":"https://i.gyazo.com/54ddd66424b6efe51e8acc1d6af67a8c.png"},
"user1":{"username":"user1","password":"user1","gameHistory":[1], "friends":[], "isPublic":true, "profilePic":""},
"user2":{"username":"user2","password":"user2","gameHistory":[2], "friends":[], "isPublic":true, "profilePic":""},
"user3":{"username":"user3","password":"user3","gameHistory":[3], "friends":[], "isPublic":true, "profilePic":""}}
2nd file:
{"0":{"id":"0","mode":"2players","creator":"user0"},
"1":{"id":"1","mode":"2players","creator":"user0"},
"2":{"id":"2","mode":"2players","creator":"user0"},
"3":{"id":"3","mode":"2players","creator":"user0"}}

I am calling these two files through a javascript file using

let games = require("./games.json"); //load initial games
let users = require("./users.json"); //load initial users

You can read it by fs.

"use strict";

const fs = require("fs");

let rawdata = fs.readFileSync("./games.json");
let games = JSON.parse(rawdata);
console.log(games);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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