HI guys,
I seem to be having an issue declaring a json string.
The original string I have is this:
var jsonString = '{ "details" : [' +
'{"text":"Lorem ipsum dolor sit amet, consectetur 0", "firstName":"Jack0", "surname":"Dee0", "moreInfo":"This is extra information"},' +
'{"text":"Lorem ipsum dolor sit amet, consectetur 1", "firstName":"Jack1", "surname":"Dee1", "moreInfo":"This is extra information"},' +
']}';
and if I parse it
jsonStringParsed = JSON.parse(jsonString);
console.log(jsonStringParsed);
I get what I expected in the console, two objects:
details:Array[2]
0:Object
firstName:"Jack0"
moreInfo:"This is extra information"
surname:"Dee0"
text:"Lorem ipsum dolor sit amet, consectetur 0"
1:Object
firstName:"Jack1"
moreInfo:"This is extra information"
surname:"Dee1"
text:"Lorem ipsum dolor sit amet, consectetur 1"
The problem is that after I parse it and loop through the objects in jsonStringParsed I don't want to have to use the syntax jsonString.details.xxxx
, but I want direct access to the object, something like this instead jsonString.xxxx
(the reason doesn't matter too much at this stage as this is part of a much larger application) so I decided to change the declaration of the json string to this:
var jsonString =
'{"text":"Lorem ipsum dolor sit amet, consectetur 0", "firstName":"Jack0", "surname":"Dee0", "moreInfo":"This is extra information"},' +
'{"text":"Lorem ipsum dolor sit amet, consectetur 1", "firstName":"Jack1", "surname":"Dee1", "moreInfo":"This is extra information"}';
But this doesn't work, and I don't quite understand why as nowehere - not that I could find it at least - said that a json string can't be declared like that, quite the contrary in fact. Does anybody have any idea why it doesn't work?
The error I get is:
Uncaught SyntaxError: Unexpected token , in JSON at position 132
at JSON.parse (<anonymous>)
at script.js:13
and the position 132 points directly to the comma in here
This is extra information"},{"text":"Lorem ipsum dolor sit amet