gpt4 book ai didi

javascript - 将 JSON 文件转换为 Javascript 对象

转载 作者:太空宇宙 更新时间:2023-11-04 15:26:44 25 4
gpt4 key购买 nike

我正在尝试将此 json 文件转换为 javascript 对象,然后以表格格式显示它(目前不要担心表格的外观,一旦解决,我会修复它。 ) 我看不出我做错了什么无法检索 json 对象,因为它们在程序运行时打印在控制台中。 statechangehandler 的两次回调存在语法错误,但在接下来的两次回调中,打印了 json 对象,不太清楚为什么。任何帮助将不胜感激,在此先感谢。

问题2.html:

<html>

<head>
<title>Question 2</title>
<script>
function makeAjaxQueryVideo() {

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {
readyStateChangeHandler(xhttp);
};

xhttp.open("GET", "Question2.json", true);
xhttp.send();
}

function readyStateChangeHandler(xhttp) {
console.log("Callback function readyStateChangeHandler is executed");
console.log("readyState = " + xhttp.readyState);
console.log("status = " + xhttp.status);
console.log("responseText:");
console.log(xhttp.responseText);

handleStatusSuccess(xhttp);
}

function handleStatusFailure(xhttp) {
var displayDiv = document.getElementById("display");
displayDiv.innerHTML = "XMLHttpRequest failed: status " + xhttp.status;
}

function handleStatusSuccess(xhttp) {
var jsonText = xhttp.responseText;

var videoObj = JSON.parse(jsonText);
console.log(videoObj);
console.log("title is " + videoObj.title);

displayVideo(videoObj);
}

function displayVideo(videoObj) {

var html = "<h2>Stock Market Activity " + videoObj.queryTime + "</h2>";
html += "<table border='1'>";
html += "<tr><th>Stock</th><th>Value</th><th>Change</th><th>Net / %</th></tr>";
for (var i = 0; i < videoObj.result.length; i++) {
var videoObj1 = videoObj.result[i];
html += "<tr>";
html += "<td><b>" + videoObj1.title + "</b></td>";
html += "<td align='right'>" + videoObj1.channel + "</td>";
html += "<td style='color:green' align='right'>";
html += videoObj1.view;
html += "<img src='stockUp.png' />";
html += "</td>";
html += "<td align='right'>" + videoObj1.link + "%</td>";
html += "</tr>";
}
html += "</table>";

var displayDiv = document.getElementById("display");
displayDiv.innerHTML = html;
}
</script>
</head>

<body>
<button onClick="makeAjaxQueryVideo()">Get Search Result</button>
<div id="display"></div>
</body>

</html>

问题2.json:

{
"result": {
"searchKeyword": "Mathematics",
"video": [
{
"title": "Chaos Game",
"channel": "Numberphile",
"view": "428K",
"link": "http://www.youtube.com/watch?v=kbKtFN71Lfs",
"image": "http://i.ytimg.com/vi/kbKtFN71Lfs/0.jpg",
"length": "8:38"
},
{
"title": "Australian Story: Meet Eddie Woo, the maths teacher you wish
you'd had in high school",
"channel": "ABC News (Australia)",
"view": "223K",
"link": "http://www.youtube.com/watch?v=SjIHB8WzJek",
"image": "http://i.ytimg.com/vi/SjIHB8WzJek/0.jpg",
"length": "28:08"
},
{
"title": "Ham Sandwich Problem",
"channel": "Numberphile",
"view": "557K",
"link": "http://www.youtube.com/watch?v=YCXmUi56rao",
"image": "http://i.ytimg.com/vi/YCXmUi56rao/0.jpg",
"length": "5:53"
},
{
"title": "Magic Square Party Trick",
"channel": "Numberphile",
"view": "312K",
"link": "http://www.youtube.com/watch?v=aQxCnmhqZko",
"image": "http://i.ytimg.com/vi/aQxCnmhqZko/0.jpg",
"length": "3:57"
},
{
"title": "The 8 Queen Problem",
"channel": "Numberphile",
"view": "909K",
"link": "http://www.youtube.com/watch?v=jPcBU0Z2Hj8",
"image": "http://i.ytimg.com/vi/jPcBU0Z2Hj8/0.jpg",
"length": "7:03"
}
]
}
}

最佳答案

There is a syntax error for two callbacks to the statechangehandler, but then on the next two callbacks, the json objects are printed, not quite sure why.

每次状态改变时,您都会调用 handleStatusSuccess ……甚至在它达到成功状态之前。

您可能应该改用 load 事件处理程序。

function makeAjaxQueryVideo() {
var xhttp = new XMLHttpRequest();
xhttp.addEventListener("load", handler)
xhttp.open("GET", "Question2.json");
xhttp.send();
}

function handler() {
console.log("Callback function readyStateChangeHandler is executed");
console.log("readyState = " + this.readyState);
console.log("status = " + this.status);
console.log("responseText:");
console.log(this.responseText);
handleStatusSuccess(this);
}

关于javascript - 将 JSON 文件转换为 Javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58549294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com