json - 我无法获取 JSON 数据
问题描述
我正在尝试包含 JSON 数据,但我的跨度为空。你知道我哪里错了吗?刚才提到我有访问密钥,当我调用 JSON 时,我将它包含在 url 中。
JSON
{
"status": true,
"msg": "successfully",
"response": [
{
"id": "1",
"price": "1.1111",
"change": "+0.0008",
"chg_per": "+0.07%",
"last_changed": "2020-01-09 14:12:02",
"symbol": "EUR/USD"
}
],
"info": {
"server_time": "2020-01-09 14:12:06 UTC"
}
}
JS
<script>
const api_url = "https://fcsapi.com/api/forex/latest?id=1"
async function getFX() {
const response = await fetch(api_url);
const data = await response.json();
const { price } = data;
document.getElementById('bid').textContent = price;
}
getFX();
setInterval(getFX, 3000);
</script>
HTML
<span class="py-1 px-2 w-full block rounded bg-dark-800 flex justify-end items-center" id="bid">
<img class=" ml-1 mt-0" src="images/svg/arrowDown.svg" style="width: 9px;height: 10px;">
</span>
解决方案
你应该这样做document.getElementById('bid').textContent = JSON.stringify(price)
。因为您首先必须将该对象转换为字符串。
或者你也可以先试试看price
有没有东西再试试
document.getElementById('bid').innerText = JSON.stringify(price)
推荐阅读
- javascript - 无法保存 Ms Teams 连接器
- python - 在 python 中并行训练两个 keras 模型
- java - 在运行时更改 Spring RequestMapping 注释值
- excel - 一次查找和删除
- autodesk-forge - Autodesk Forge Viewer:如何设置对象遮挡
- python - 使用经度和纬度在 xarray 中绘图
- python - 使用 webbrowser 自动向多个用户发送 whatsapp 消息(而不是使用 selenium)
- batch-file - 如何在 .bat 文件中延迟毫秒
- google-smart-home - 本地履行应用程序如何在 Google Nest 设备中运行
- flutter - 如何在 Flutter 中的选项卡(TabBar)之间保持状态?