admin管理员组文章数量:1287515
I have a JSON file I use to store quotes for some Quote Generator I'm building. Recently I got this error in my terminal (see screenshot below).
Expected a JSON object, array or literal.json
This is what my JSON looks like
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."
}]
I've tried all I could. but the error keeps ing, what could be wrong?
I have a JSON file I use to store quotes for some Quote Generator I'm building. Recently I got this error in my terminal (see screenshot below).
Expected a JSON object, array or literal.json
This is what my JSON looks like
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."
}]
I've tried all I could. but the error keeps ing, what could be wrong?
Share Improve this question edited Jan 21, 2019 at 14:39 thlpswm 453 bronze badges asked Dec 17, 2018 at 22:10 Bolaji AyodejiBolaji Ayodeji 471 gold badge3 silver badges9 bronze badges 12- 1 It'd help to see the stack trace and what led to the error. What action are you trying to perform that yields this error? – proton Commented Dec 17, 2018 at 22:13
- I'm trying to store quotes in JSON and serve to HTML. – Bolaji Ayodeji Commented Dec 17, 2018 at 22:15
- 2 sigh.. you put javascript in a json file. remove the javascript and leave the json. – Kevin B Commented Dec 17, 2018 at 22:22
-
1
Yeah! Was about to ask that, is the data contained in
.js
file or.json
file? – proton Commented Dec 17, 2018 at 22:22 -
1
a JSON file should not contain
data =
– MrUpsidown Commented Jun 3, 2020 at 18:59
6 Answers
Reset to default 12You 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.
I had a similar problem but finally figured it out like this:
firstly, you need to make sure you have an eslintrc.json
file, and then inside that json file you add this json object:
{
"extends": [
"plugin:react/remended",
"plugin:@typescript-eslint/remended",
"plugin:prettier/remended"
]
}
which also works if you've a typescript or prettier errors.
The error occurred for me but mine one just gone by changing file extension form json to js.
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));
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);
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.
本文标签: javascriptExpected a JSON objectarray or literaljsonStack Overflow
版权声明:本文标题:javascript - Expected a JSON object, array or literal.json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741267138a2368691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论