gpt4 book ai didi

javascript - 完全陷入如何在 HTML 中显示 JSON 的问题。编辑 : I know how i want to format it, 我只是无法让它输出除原始 JSON 之外的任何内容

转载 作者:行者123 更新时间:2023-12-01 07:37:31 24 4
gpt4 key购买 nike

我已经为此工作了几个小时,到处搜索,似乎无法解决它。我留下了 API URL,所以如果你想看一下(它是公开的)。我很累,所以如果我没有理解,请告诉我。我正在尝试基本上为新闻制作自定义布局。但我不能用它做任何事情,我只有原始的 JSON,并且我已经尝试了很多。如果你能给出一个例子,比如带有新闻文章名称的标题,或者简单的东西,我会弄清楚的,但现在,我很沮丧,在原地打转,哈哈。谢谢!

{
"location": {
"long": -106.346771,
"countryOrRegion": "Canada",
"provinceOrState": null,
"county": null,
"isoCode": "CA",
"lat": 56.130366
},
"updatedDateTime": "2020-03-31T04:46:00.3795966Z",
"news": [
{
"path": "_news/2020-03-31-canada-post-workers-customers-seeing-heightened-safeguards.md",
"title": "Canada Post workers, customers seeing heightened safeguards",
"excerpt": "COVID-19 ...",
"heat": null,
"tags": [
"CA"
] ,
"type": "article",
"webUrl": "https://nnsl.com/yellowknifer/canada-post-workers-customers-seeing-heightened-safeguards/",
"ampWebUrl": null,
"cdnAmpWebUrl": null,
"publishedDateTime": "2020-03-30T21:00:00-07:00",
"updatedDateTime": null,
"provider": {
"name": "Northern News Services",
"domain": "nnsl.com",
"images": null,
"publishers": null,
"authors": null
},
"images": null,
"locale": "en-us",
"categories": [
"news"
],
"topics": [
"Coronavirus in Canada",
"Coronavirus"
]
},
{
"path": "_news/2020-03-31-in-canada-and-abroad-covid-19-super-spreaders-could-be-anywhere.md",
"title": "In Canada and abroad, COVID-19 super-spreaders could be anywhere",
"excerpt": "In Canada, while no specific individual has yet been identified as super-spreader ...
Scientists are researching how much of a role silent carriers of COVID-19 - those who exhibit no symptoms - play in unknowingly spreading the disease. This is why self-isolation is important, Riskin said. \"It’s a reminder that for Canadians, we all have ...",
"heat": null,
" ...etc

糟糕,您需要 key 才能查看该文件。我将粘贴一个条目。它是免费的。

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="./jquery-3.4.1.min.js"></script>
<style>
countryOrRegion: font-size: 20px;

</style>
<title></title>
</head>
<body>

<div id="demo"></div>


<script>
$.ajax({
type: "GET",
url: "https://api.smartable.ai/coronavirus/news/CA",
// Request headers
beforeSend: function(xhrObj) {
xhrObj.setRequestHeader("Cache-Control", "no-cache");
xhrObj.setRequestHeader("Subscription-Key", "my free key");
},
})

.done(function (data) {

var myJSON = JSON.stringify(data);

document.getElementById("demo").innerHTML = myJSON;

})
</script>

</body>

最佳答案

按原样使用响应怎么样?我更改了您的代码以使用来自 reqres.in 的免费 API 调用,然后使用结果中的特定信息。

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<style>
countryOrRegion: font-size: 20px;

</style>
<title></title>
</head>
<body>

<div id="demo">
<p id="first-name"></p>
<p id="last-name"></p>
</div>


<script>
$.ajax({
type: "GET",
url: "https://reqres.in/api/users/1",
// Request headers
beforeSend: function(xhrObj) {
xhrObj.setRequestHeader("Cache-Control", "no-cache");
xhrObj.setRequestHeader("Subscription-Key", "my free key");
},
})

.done(function (data) {
console.log('Your response', data)
document.getElementById("first-name").innerHTML = data.data.first_name;
document.getElementById("last-name").innerHTML = data.data.last_name;
})
</script>

</body>

因此,您可以使用 data.location.longdata.location.lat 或您需要在 API 调用响应中显示的任何内容。

关于javascript - 完全陷入如何在 HTML 中显示 JSON 的问题。编辑 : I know how i want to format it, 我只是无法让它输出除原始 JSON 之外的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60944083/

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