I'm doing a web application of a test and I have a JSON file with the questions, but I have an error trying to convert the JSON file to a javascript object using the JSON.parse()
function.
Here is the code:
function escogerPregunta (numero) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(xhr.status === 200) {
respuestaJSON = JSON.parse(xhr.responseText); //Aqui está el error
console.log(respuestaJSON);
/*
var p = respuestaJSON.preguntas[numero].pregunta;
var r = respuestaJSON.preguntas[numero].respuestas;
var s = '';
s += '<h1>' + p + '</h1>';
for (var i = 0; i < r.length; i++) {
s += '<input name="opciones" type=radio id="' + i + '">';
s += '<label for="' + i + '">' + r[i] + '</label><br>';
}
$('#pregunta').innerHTML = s;
$('#pregunta').hide().fadeIn(700);
radios[0].checked = 'checked';
*/
}
else {
alert(xhr.status);
}
};
xhr.open('GET', 'http://localhost/quiz/data.json', true);
xhr.send(null);
}
And here's the JSON file:
{
"preguntas": [{
"pregunta": "¿Cuál es la montaña más alta del mundo?",
"respuestas": ["Kijimanjaro", "Monte Everest", "Makalu"],
"respuesta_correcta": 1
},
{
"pregunta": "¿Quién vivía en el 221B de Backer Street?",
"respuestas": ["Sherlock Holmes", "Truman Capote", "Philip Marlowe", "Arthur Conan Doyle"],
"respuesta_correcta": 0
},
{
"pregunta": "¿En que año descubrió Colón América?",
"respuestas": ["1502", "1492", "1946", "1488"],
"respuesta_correcta": 1
},
{
"pregunta": "¿En qué año se estrenó la película de Disney <q>Pinocho</q>?",
"respuestas": ["1940", "1950", "1952", "1946"],
"respuesta_correcta": 0
},
{
"pregunta": "¿Quién descubrió el ADN?",
"respuestas": ["Tristan Tzara", "Alfred Jarry", "James Watson y Francis Crick", "Friedrich Miescher"],
"respuesta_correcta": 2
},
{
"pregunta": "¿De que deporte es el kemari uno de los principales antecesores?",
"respuestas": ["Fútbol", "Ténis", "Rugby", "Karate"],
"respuesta_correcta": 0
},
{
"pregunta": "¿Cuál es la capital de Brasil",
"respuestas": ["Sao Paulo", "Brasilia", "Río de Janeiro"],
"respuesta_correcta": 1
},
{
"pregunta": "¿Cómo se llama el protagonista de <q>El código Da Vinci<q/> de Dan Brown?",
"respuestas": ["Robert Langdom", "Tom Hanks", "Mr. White", "Robert Black"],
"respuesta_correcta": 0
},
{
"pregunta": "¿En que guerra participó Juana de Arco?",
"respuestas": ["La guerra de los 30 años", "La guerra de los 100 años", "Guerras napoleónicas"],
"respuesta_correcta": 1
},
{
"pregunta": "¿Cómo se llamaba el personaje que interpretaba Al Pacino en Scarface?",
"respuestas": ["Sonny Montana", "Tony Montana", "Michael Corleone", "Frank Slade"],
"respuesta_correcta": 1
},
{
"pregunta": "Quién fue el primero en decir que la tierra gira alrededor del sol?",
"respuestas": ["Copérnico", "Galileo", "Aristarco de Samos", "Kepler"],
"respuesta_correcta": 2
},
{
"pregunta": "¿Cuántas finales del mundo jugó la Selección Argentina de fútbol?",
"respuestas": ["Cinco", "Seis", "Cuatro", "Tres"],
"respuesta_correcta": 2
},
{
"pregunta": "¿Cuál es el segundo continente más grande del mundo?",
"respuestas": ["África", "Antártida", "Norte América", "Europa"],
"respuesta_correcta": 0
},
{
"pregunta": "¿Cómo se llama a la gente que no posee magia en la saga de Harry Potter?",
"respuestas": ["Humano", "Simplón", "Impuro", "Muggles"],
"respuesta_correcta": 3
},
{
"pregunta": "¿En que país nació Adolf Hitler?",
"respuestas": ["Polonia", "Austria", "Alemania", "Suiza"],
"respuesta_correcta": 1
},
{
"pregunta": "¿Qué grupo interpretaba la canción <q>Smells like teen spirit</q>?",
"respuestas": ["Los Beatles", "Led Zeppelin", "Nirvana", "Los Rolling Stones"],
"respuesta_correcta": 2
},
{
"pregunta": "¿Qué órgano del cuerpo humano produce la bilis?",
"respuestas": ["Hígado", "Páncreas", "Intestino delgado", "Riñon"],
"respuesta_correcta": 0
},
{
"pregunta": "¿Cuántos jugadores componen un equipo de rugby?",
"respuestas": ["11", "12", "15", "21"],
"respuesta_correcta": 2
}
]
}
Google chrome tells me it's a syntax error, but I do not see where that error is. I tried to delete this line of code and the error disappears respuestaJSON = JSON.parse(xhr.responseText);
The google chrome console gives this error:
Uncaught SyntaxError: Unexpected token; in JSON at position 3101 at JSON.parse () at XMLHttpRequest.xhr.onload