gpt4 book ai didi

javascript - 将 json 值打印到 html 中

转载 作者:行者123 更新时间:2023-11-28 13:16:28 25 4
gpt4 key购买 nike

我从控制台中的 json 获取这些值:Object {test: 0, howmuch: 0, day: 22, Calls: Array[0]}

但是我怎样才能在我的 html 上打印它呢?我尝试做 jquery 但我无法得到。从 url 获取这些值对我来说也很简单吗?

jquery:

$(document).ready(function () {
$('#get-data').click(function () {
var showData = $('#show-data');

$.getJSON('real-data.json', function (data) {
console.log(data);
alert(data);

showData.empty();

});

showData.text('Loading the JSON file.');
});
});

json:

{
"test": 0,
"howmuch": 0,
"day": 22,
"calls": [

]
}

html:

<!DOCTYPE html>
<html>
<head>
<script data-require="jquery@2.1.1" data-semver="1.9.1" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>

<script src="real-data.js"></script>
<style>body{ background: #F9F9FA; }</style>
</head>

<body>
<a href="#" id="get-data">Get JSON data</a>
<div id="show-data"></div>
</body>
</html>

最佳答案

不确定您是否正在寻找类似的东西。无法使用 $.getJSON 因此假设 var x 具有所需的值。

var x ={
"test": 0,
"howmuch": 0,
"day": 22,
"calls": [
]
}

$(document).ready(function () {
$('#get-data').click(function () {
var _jsonString = "";

for(var key in x){
_jsonString +="key "+key+" value "+x[key]+ '</br>';
}

$("#show-data").append(_jsonString)
});
});

Jsfiddle

关于javascript - 将 json 值打印到 html 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548661/

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