Expected A Json Object, Array Or Literal.json
Solution 1:
You just need to format your file a bit like this:
{"data":[{"number":"1","author":"Von R. Glitschka","quote":"The client may be king, but he's not the art director."},{"number":"2","author":"Frank Capra","quote":"A hunch is creativity trying to tell you something."},{"number":"3","author":"Steven Heller","quote":"As a profession, graphic designers have been shamefully remiss or ineffective about plying their craft for social or political betterment."}]}
And save this with a .json
extension.
Solution 2:
It looks like, given the data you have and the code you are using to get a random number, your number is often exceeding the number of objects you have in your array.
For example,
Math.floor(Math.random() * 50)
Could end up setting random to 13, which greatly exceeds the number of values in your array.
If you would like to get a random number between 0 and 2, you can use:
random = Math.floor(Math.random() * Math.floor(3));
Solution 3:
An alternate approach to this is
data = [
{
"number": "1",
"author": "Von R. Glitschka",
"quote": "The client may be king, but he's not the art director."
},
{
"number": "2",
"author": "Frank Capra",
"quote": "A hunch is creativity trying to tell you something."
},
{
"number": "3",
"author": "Steven Heller",
"quote": "As a profession, graphic designers have been shamefully remiss or ineffective about plying their craft for social or political betterment."
}];
console.log(data);
var random = Math.floor(Math.random() * data.length);
console.log(data[random].quote);
console.log(data[random].author);
Solution 4:
Well first, this isn't exactly a JSON format. This is an array of objects.
Your JSON can not have a variable assignment like the one you have
var data = .....
Depending on why you are getting the error or what you intend to do with the data. You have 2 options:
Convert this array into an acceptable JSON object, like so:
$ JSON.stringify(data)
.You can work with this data directly as an array that it is, just by storing it either as a js variable or in a js file. Then you can easily manipulate it like an array.
Post a Comment for "Expected A Json Object, Array Or Literal.json"