gpt4 book ai didi

javascript - SyntaxError :/Users/3x7r3m157/Development/Javascript/db. json:JSON 输入意外结束

转载 作者:行者123 更新时间:2023-12-02 22:46:51 25 4
gpt4 key购买 nike

在这个错误中,我似乎找不到,我正在读取一个名为 db.json 的 .json 文件,更新 json 并尝试写入 .json 文件。虽然我正在解析和字符串化 json,但我收到此错误:

语法错误:/Users/3x7r3m157/Development/Javascript/db.json:JSON 输入意外结束

我的代码如下:

const args = require('yargs').argv;
const fs = require('fs');
const util = require('util');
const leaderboard = require('./db.json')

const addCompetitor = (name) => {
leaderboard[name] = { points: [], times: [] }
console.log(leaderboard)
return leaderboard
}

console.log(leaderboard)

const addCompetitorTimes = (data) => {
console.log(data)
//this method takes a string of 'competitorName_time'
//parses it into substrings 'name' & 'time'.

//Afterwards it iterates through time in minutes and seconds as a string
//in the format of '00:00', then parses the string into substrings
//of minutes and seconds delimited by the ':' character.
//Thereafter it converts the string to integeters and converts the time
//into time in seconds for easy comparison of times.

//Finally it assigns the time to the competitor in the leaderboard and
//writes to json file db.json

let parser = 0;
var competitor = '';
let times = '';
let seconds = '';
let minutes = '';
let timeInSeconds = 0;

for (let i = 0 ; i < data.length ; i ++) {
if (parser == 0 && data[i] == '_') {
parser ++
continue
}
if (parser == 0) {
//Stack Overflow peeps, weird error right hurrr:
competitor += data[i]
}
if (parser == 1) {
times += data[i]
}
}

parser = 0

for (let i = 0 ; i < times.length ; i ++) {
if (parser == 0 && times[i] == ':') {
parser ++
continue
}
if (parser == 0) {
minutes += times[i]
}
if (parser == 1) {
seconds += times[i]
}
}

seconds = parseInt(seconds);
minutes = parseInt(minutes);

let minutesInSeconds = minutes * 60

seconds = minutesInSeconds + seconds
leaderboard[competitor].times = seconds

console.log(leaderboard)

fs.writeFileSync('./db.json', JSON.stringify(leaderboard));

return leaderboard
}

addCompetitorTimes(args.competitorTimes)

db.json:

{"Atlas":{"points":0,"times":0}}

完整堆栈跟踪错误:

internal/modules/cjs/loader.js:800
throw err;
^

SyntaxError: /Users/3x7r3m157/Development/Javascript/db.json: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.Module._extensions..json (internal/modules/cjs/loader.js:797:27)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/Users/3x7r3m157/Development/Javascript/leaderboard.js:4:21)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

最佳答案

问题是“require”为你解析json,但你用json.parse多余地解析它。它已经是一个对象并且失败了。只是不要双重解析。有要求就够了。

关于javascript - SyntaxError :/Users/3x7r3m157/Development/Javascript/db. json:JSON 输入意外结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364129/

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