1

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.

1
  • Have you tried for ( ii in data ) { console.log(data[ii]); }? Commented Jun 20, 2014 at 11:27

2 Answers 2

2

There is no array returned in JSON. With above JSON sample you can try below code

refer to this fiddle http://jsfiddle.net/JS795/

You can use below code

var zoneHtml = '';

for(var zoneKey in data) {
    zoneHtml+='<div class="zone">';
    zoneHtml+= ('<h1>'+zoneKey+'</h1>');
    var countries = data[zoneKey];

    for(var country in countries) {
        zoneHtml+='<div class="countries">';
        zoneHtml+=('<h2>'+country+'</h2>');
        var ports =  countries[country];
        zoneHtml+='<ul class="port">';

        for(var port in ports) {
            zoneHtml+=('<li>'+port+':'+ports[port] +'</li>');
        }

        zoneHtml+='</ul>';
        zoneHtml+='</div>';
    }
    zoneHtml+=('</div>');
}

$("#zone").html(zoneHtml);
Sign up to request clarification or add additional context in comments.

2 Comments

if "UK":{"UK1":"Portsmouth"}, contains additional sub array e.g. "UK":{"UK1":["Portsmouth","Y","Bob","1234"]}, how would you get that information? would that be another for loop on the port variable, btw thanks for your very readable help
yes, that will be additional standard for loop with one counter on ports[port]
0

The following code output the desired html :

    var content = '';
    for( i in data) {
        var subcontent = '';
        for( subi in data[i]) {
            var subsubcontent = '';
            for( subsubi in data[i][subi]) {
                subsubcontent += '<li>'+subsubi+' : '+data[i][subi][subsubi]+'</li>';
            }
            subcontent += '<div class="country"><h2>'+subi+'</h2><ul class="port">'+subsubcontent+'</ul></div>';
        }
        content += '<div class="zone"><h1>'+i+'</h1>'+subcontent+'</div>';
    }

jQuery's each is not needed here. Let me know if you need more informations on that code.

3 Comments

thanks for your help, have discovered that I have a few more nested layers to dig through
whats the best way to dump for reading the object data? in php I'd use print_r or var_dump but I'm not sure for javascript
console.log(JSON.stringify(json_object))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.