为什么JSON数据显示;“未定义”;用JavaScript?
嘿,我试图用我的JSON文件显示一些引号。但它显示的是未定义的。任何人都可以检查下面的html和JavaScript 我的Javascript stat.json的内容是为什么JSON数据显示;“未定义”;用JavaScript?,javascript,json,scripting,Javascript,Json,Scripting,嘿,我试图用我的JSON文件显示一些引号。但它显示的是未定义的。任何人都可以检查下面的html和JavaScript 我的Javascript stat.json的内容是 [ { "quotes":"Do what is right not what is easy." }, { "quotes":"Hey there, Whatsapp is using me.&quo
[
{
"quotes":"Do what is right not what is easy."
},
{
"quotes":"Hey there, Whatsapp is using me."
},
{
"quotes":"Too Busy"
},
{
"quotes":"Only you can give me that feeling."
},
{
"quotes":"I had a horribly busy day in converting oxygen into carbon dioxide"
},
{
"quotes":"Be yourself; everyone else is already taken"
},
{
"quotes":"Your attitude may hurt me, But main can Kill You!!"
},
{
"quotes":"Love is Blind, be careful."
},
{
"quotes":"'SUCCESS' is depend on U."
},
{
"quotes":"If you love someone set it free."
},
{
"quotes":"Love is sweet, When it's new. But it is sweeter when it's true."
},
{
"quotes":"Where ther is love, there is life."
},
{
"quotes":"Not always 'Available' try your luck.."
},
{
"quotes":"I am not changed it's just I grew up and you should try too."
},
{
"quotes":"The biggest slap to your enimies is your success."
},
{
"quotes":"Born to express, not to impress."
},
{
"quotes":"When nothing goes right! go left."
},
{
"quotes":"I allow myself to be badass confident in all that I do."
},
{
"quotes":"Sometimes you succeed and other time you learn."
},
{
"quotes":"A true friend sees the first tear, catches the second and stops the third."
},
{
"quotes":"We carry our childhood with us!!"
},
{
"quotes":"Childhood is the most beautiful of all lige's season!!"
}
]
这些引号要显示在Javascript创建的
元素中。我的javascript中的位置
const retrieveAllQuotes = async function() {
// here we are making a network call to your api
const response = await fetch('/stat.json');
contents.innerHTML = paged.map(record => `<div class='latestatus'><p class='copytxt'>${record.quotes}</p><div> <button class="copystatus btn">Copy</button></div></div>`).join('');
resultEl.append(contents);
};
contents.innerHTML=paged.map(record=>`${record.quotes}
Copy`).join(“”);
结果追加(内容);
};
如果我运行这段代码,它会显示所有未定义的引号。如果有人有时间,请运行上述代码并测试两次。我终于得到了答案。这是制图中的一个错误。这是由@Barmar回答的
const results = data.map(val => ({quotes: val.quotes}));
拆下这部分
// finally go over the array and return new object with renamed key
const results = data.map(val => ({quotes: val.status}));
return results;
返回
数据
,json中不存在状态
字段,重新映射json以获得相同的模式是完全无用的。这不是有效的json,缺少[
开头。我想这是一个复制错误,因为你没有得到JSON解析错误。你的JSON没有状态
键。它们都只有引号
键。你的地图不会匹配任何属性val.status
应该是val.quotes
。我忘记添加了[是的,谢谢地图上有个错误谢谢@Barmar
const results = data.map(val => ({quotes: val.quotes}));
// finally go over the array and return new object with renamed key
const results = data.map(val => ({quotes: val.status}));
return results;