I'm calling a json webservice with jQuery and it returns a multi-dimension array.
The context of the values are known but not provided as key values
Zone,
Country,
Port Code,
Port Name
JSON return sample:
{
"Europe":
{
"UK":{"UK1":"Portsmouth"},
"France":{"FR1":"Caen","FR2":"Calais"}
}.
"Americas":
{
"USA":{"US1":"Portsmouth2"},
"Canada":{"CA1":"Caen2","CA2":"Calais2"}
}
}
I have the returned JSON object as follows (url removed), I tried parseJSON but that throws errors as the data is already 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);
}
});
I want to loop through the array levels and then stick the answer into nested div tags
<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>
I thought I should be to do something like alert(data[0]); but that just says object
Not sure on how to use jQuery each for this.
for ( ii in data ) { console.log(data[ii]); }?