最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

SyntaxError:Users3x7r3m157DevelopmentJavascriptdb.json:JSON输入的意外结尾

IT培训 admin 5浏览 0评论

SyntaxError:/Users/3x7r3m157/Development/Javascript/db.json:JSON输入的意外结尾

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

SyntaxError:/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(argspetitorTimes)

db.json:

{"Atlas":{"points":0,"times":0}}
回答如下:

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

SyntaxError:/Users/3x7r3m157/Development/Javascript/db.json:JSON输入的意外结尾

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

SyntaxError:/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(argspetitorTimes)

db.json:

{"Atlas":{"points":0,"times":0}}
回答如下:

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

发布评论

评论列表 (0)

  1. 暂无评论