我在我的控制檯正從我的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元素來實現。 –