-1

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.

1 Answers1

1

You can't split your String values over multiple lines like that in JSON.

{
"src":"Pictures/Comics/spi.jpg",
"descr":"Spider man is a nice comic \
        The main spider-man comic \
        ended with Peter Parker's \
        death."
}

Needs to be changed to this...

{
"src": "Pictures/Comics/spi.jpg",
"descr": "Spider man is a nice comic The main spider-man comic ended with Peter Parker's death."
}

You will need to change some of your other entries too. See here Multiline strings in JSON

Community
  • 1
  • 1
Thomas Powell
  • 115
  • 1
  • 12
  • Thanks, It worked just fine in one line. Also, according to what I've understood from your link, there is a way to do a multiline string. It doesn't matter now, but it I would have wanted to do so, how could I have done? – ATrashInTheWorld May 14 '17 at 23:20
  • `JSON` does not support multiline string values. If you are wanting the value to render on multiple lines you can encode newline '`\n`' characters into the string value. If you are wanting the value in the `JSON` document to appear across multiple lines for readability you could change the value type to an `Array` of `String`s and then join them in your code. – Thomas Powell May 15 '17 at 09:04