我在我的控制台正从我的JSON这些值:Object {test: 0, howmuch: 0, day: 22, calls: Array[0]}
打印JSON值到HTML
但我怎么可以打印此对我的HTML?我试图做jQuery,但我无法得到。对于我来说,从网址获取这些值也很简单?
的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="[email protected]" 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>
好了,你怎么想显示呢?您需要将它写入HTML中某些元素的内容(可能是#show-data div),以显示您想要的字段。 –
在div中显示 – Raduken
您没有json字符串,您有一个JavaScript对象。如果你想再次得到字符串,你需要使用'JSON.stringify'。但大概你宁愿格式化它看起来比这更好。但是你需要决定你想如何出现,然后附加html元素来实现。 –