我使用jQuery調用json webservice,它返回一個多維數組。如何從嵌套對象構建HTML?
的值的上下文中已知的,但不作爲鍵值
Zone,
Country,
Port Code,
Port Name
JSON返回樣本提供:
{
"Europe":
{
"UK":{"UK1":"Portsmouth"},
"France":{"FR1":"Caen","FR2":"Calais"}
}.
"Americas":
{
"USA":{"US1":"Portsmouth2"},
"Canada":{"CA1":"Caen2","CA2":"Calais2"}
}
}
我有返回的JSON對象如下(URL移除),我試圖parseJSON,但由於數據已經是JSON而引發錯誤
$.ajax({
type: "POST",
dataType: "json",
url: "linktourl",
data: "user="+user,
success: function (data) {
alert(data);
console.log(data);
$('#loading').html('<h1>Returned Data:</h1>'+data);
}
});
我想遍歷數組水平,然後粘到答案嵌套div標籤
<div class="zone">
<h1>Europe</h1>
<div class="country">
<h2>UK</h2>
<ul class="port">
<li>UK1 : Portsmouth</li>
</ul>
</div>
<div class="country">
<h2>France</h2>
<ul class="port">
<li>FR1 : Caen</li>
<li>FR2 : Calais</li>
</ul>
</div>
</div>
<div class="zone">
<h1>Americas</h1>
<div class="country">
<h2>USA</h2>
<ul class="port">
<li>US1 : Portsmouth2</li>
</ul>
</div>
<div class="country">
<h2>Canada</h2>
<ul class="port">
<li>CA1 : Caen2</li>
<li>CA2 : Calais2</li>
</ul>
</div>
</div>
,我想我應該做類似警報(數據[0]);但只是說對象
不確定如何使用jQuery的每一個這個。
你試過'的(數據II){執行console.log(數據[II]); }'? – bloodyKnuckles