JSON以下JSON成HTML格式:如何顯示狀態,使用jQuery
代碼:
for (var i = 0; i < data.data.length; i++) {
var output ="" + data.data[i].location.location.country + "";
}
JSON以下JSON成HTML格式:如何顯示狀態,使用jQuery
代碼:
for (var i = 0; i < data.data.length; i++) {
var output ="" + data.data[i].location.location.country + "";
}
假設您的解析的JSON變量稱爲數據,所以:
var data = JSON.parse("<json-string>");
, 然後你可以得到這樣的狀態:
var state = data.data[0].location.location.state;
然後,您可以使用顯示HTML中的狀態變量的jQuery像這樣:$("div.container").html(state)
這jQuery代碼假設你已經包括了腳本參考jQuery庫和你的HTML確實有一類元素容器名稱,比如``。
如果你沒有jQuery或只是不想使用它,你可以這樣做:document.write(state)
。
最後,如果你的JSON中有數據元素的集合,那麼你可以使用for循環遍歷每個數據元素,並顯示每個數據元素的狀態,像這樣:
for (var i = 0; i < data.data.length; i++) {
var output ="" + data.data[i].location.location.state + "";
$("div.container"+i).html(output);
//document.write(output + "<br/>"); // without jquery
}
最初,我們必須使用$ .parseJSON 然後解析JSON字符串,試試這個
Here response is your json string that is shown in image
var data = $.parseJSON(response);
for (var i = 0; i < data.data.length; i++) {
var output = "" + data.data[i].location.location.country + "";
}