问题描述
由于我的webpack.config文件中没有正确的加载器,因此无法解析JSON文件。我正在使用create-react-app,并且这些文件已通过脚本锁定并包含在内。昨晚当我尝试解决此问题时,我添加了webpack,这是一个错误,因为create-react-app已经为我创建了这个。我不确定自己在做什么错-无论是json文件的格式还是依赖项不正确。
import React,{ Component } from 'react';
import TriviaQuestions from '../questions'
class TriviaGame extends Component {
state = {
triviaStart: false,triviaReset: false,totalCorrect: 0,questionNumber: 0,totalQuestions: 0,question: '',answerOne: '',answerTwo: '',answerThree: '',answerFour: '',correct: '',};
newQuestion = () => {
if (this.state.triviaStart === true) {
this.setState({
question: Questions[this.state.questionNumber].question,answerOne: Questions[this.state.questionNum].answerOne,answerTwo: Questions[this.state.questionNum].answerTwo,answerThree: Questions[this.state.questionNum].answerThree,answerFour: Questions[this.state.questionNum].answerFour,correct: Questions[this.state.questionNum].correct,totalQuestions: Questions.length
})
}
};
triviaGameStart = () => {
this.setState({
triviaStart: true,questionNumber: this.state.questionNumber + 1
})
this.newQuestion()
};
};
export default TriviaGame
我的json文件-更改导出语法不会更改我是否可以从triviaGame访问该文件,我的json开始解析该文件并失败:
[
{
"question": "In Pokemon Blue/Red,whom does Gary choose if you
take Bulbasaur?"
"answerOne": "pikachu"
"answerTwo": "Eevee"
"answerThree": "Charmander"
"answerFour": "Squirtle"
"correct": "3"
},{
"question": "Low kick is super effective against which Pokemon?"
"answerOne": "Lapras"
"answerTwo": "Arcanine"
"answerThree": "Pidgeotto"
"answerFour": "Clefairy"
"correct": "4"
},{
"question": "What is the name of the Professor who gives your
first pokemon in Pokemon Red/Blue/Yellow?"
"answerOne": "Professor Elm"
"answerTwo": "Professor Birch"
"answerThree": "Professor Oak"
"answerFour": "Professor Pine"
"correct": "3"
},{
"question": "In Pokemon Yellow,at which level does pikachu learn
slam?"
"answerOne": "19"
"answerTwo": "20"
"answerThree": "21"
"answerFour": "22"
"correct": "2"
},{
"question": "In the original Pokemon series,Gyrados is which two
types?"
"answerOne": "Water/Dragon"
"answerTwo": "Flying/Dragon"
"answerThree": "Water/Flying"
"answerFour": "Dragon/Electric"
"correct": "3"
},normal type moves
are super effective against which type(s)?"
"answerOne": "Ghost"
"answerTwo": "fighting"
"answerThree": "Both of the above"
"answerFour": "None of the above"
"correct": "4"
},{
"question": "In the original Pokemon,what is the name of the
item needed to engage with Ghost type pokemon?"
"answerOne": "Silph Scope "
"answerTwo": "Cursed Trinket"
"answerThree": "Lavender Amulet"
"answerFour": "Pokeflute"
"correct": "1"
},{
"question": "Which pokemon can be caught in both the Safari Zone,as well as Cerulean Cave?"
"answerOne": "Kangaskhan"
"answerThree": "Chancey"
"answerTwo": "Dragonair"
"answerFour": "Golem"
"correct": "2"
},{
"question": "Where is Missingo located?"
"answerOne": "Viridian City"
"answerTwo": "Mt. Moon"
"answerThree": "Cinnibar Island"
"answerFour": "Rocket Hideout"
"correct": "3"
},{
"question": "Which Pokemon do I prefer?"
"answerOne": "Dragonite"
"answerTwo": "Machamp"
"answerThree": "Gengar"
"answerFour": "Alakazam"
"correct": "1"
},]
这是我收到的完整错误消息:
Failed to compile
./src/questions.json
Module parse Failed: Unexpected string in JSON at position 94 while parsing '[
{
"question": "In Pokemon Blue/R'
You may need an appropriate loader to handle this file type,currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
SyntaxError: Unexpected string in JSON at position 94 while parsing '[
{
"question": "In Pokemon Blue/R'
at JSON.parse (<anonymous>)
非常感谢您的帮助。如果可以的话,我很乐意提供更多信息。
解决方法
在每一行的末尾加上',',然后'/'返回'\ /':
{
"question": "In Pokemon Blue\/ Red,whom does Gary choose if you
take Bulbasaur?","answerOne": "Pikachu","answerTwo": "Eevee","answerThree": "Charmander","answerFour": "Squirtle","correct": "3",}