So I have a problem with my JSON. It throws me an undefined. What makes it strange is that it recognizes my first variable.
Here are the codes to make it clear.
JSON
{"Comics":[
{"comic" :
{
"src":"Pictures/Comics/dw.jpg",
"descr":"Doctor Who has a nice tv show."
}
},
{"comic" :
{
"src":"Pictures/Comics/spi.jpg",
"descr":"Spider man is a nice comic \
The main spider-man comic \
ended with Peter Parker's \
death."
}
},
{"comic" :
{
"src":"Pictures/Comics/gg.jpg",
"descr":"Power puff girls also has a \
nice tv show . \
You should try watching it."
}
},
{"comic" :
{
"src":"Pictures/Comics/v.jpg",
"descr":"V fo Vendeta is a nice \
graphical volume written by\
Allan Moor who also wrote \
Watchmen. Those books had a movie."
}
}
]
}
Here is my Javascript
var arrI = new Array(4);
var arrD = new Array(4);
var x = new XMLHttpRequest;
x.onload = function(){
if(x.status == 200){
var txt = JSON.parse(x.responseText);
for(var g = 0; g<txt.Comics.length; g++){
arrI[g] = txt.Comics[g].comic.src;
arrD[g] = txt.Comics[g].comic.descr;
alert(arrD[g]);
}
}
}
x.open("GET",'Ajax/ImgCom.json', true);
x.send(null);
The strange part is that the arrI[g] = txt.Comics[g].comic.src;
is ok. If I alert it, it gives me the src
. But the desc
is not. It alerts me undefined
. I don't understand why, after all, they both have the good path.
I tried to correct myself with https://jsonlint.com. It told me:
Error: Parse error on line 5:
....jpg", "descr": 'Doctor Who has a ni
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
I then tried to change the "
of the desc to '
, but it didn't work.
Finally, I also tried the ...=txt.Comics[g].comic[desc]
notation, but it still gave e undefined.
I corrected myself, I didn't found any typo. Maybe there are.
Thank you for your time.