0

I use ajax to get some json encoded data from a joomla component. What I receive is what I will show in next picture: enter image description here

Now there are 2 objects(aka 2 courses). Now I want to show those 2 courses(name,category and so on) to the DOM, but I dont know how using javascript/jQuery, dont know how to iterate through them. I think angular might be also a solution, but I am not that sure.

   jQuery.ajax({
        method:"POST",
        url: 'url',
        data:{filters:filters},
        jsonpCallback: 'jsonCallback',
        dataType: 'json',
        error: function(e) {
           console.log(e.message);
        }
    }).done(function(json){
        console.log(json);//this is shown in the image       
    });

Please help me, thanks!

Petre Gabriel
  • 141
  • 1
  • 2
  • 13
  • 1
    See this: http://stackoverflow.com/questions/8312459/iterate-through-object-properties – leo.fcx Sep 03 '15 at 18:25
  • Try deserializing it, then accessing the elements you want like through a for loop or however you prefer. Check out : http://stackoverflow.com/questions/6487167/deserialize-from-json-to-javascript-object – Michael Watson Sep 03 '15 at 18:25
  • `$.each(json, function(key, object) { console.log('the alias:' + object.alias); });` – Patrick DaVader Sep 03 '15 at 19:00

1 Answers1

0

To iterate through any object and child objects cycle through keys of obj. If object is found (here excluding array type objects) recursively walk that obj. Otherwise, do something with values:

function walkObj(obj) {
    var keys_arr = Object.keys(obj);
    var key_ct = keys_arr.length;
    var key, value;
    while (key_ct) {
        key = keys_arr[key_ct - 1];
        value = obj[key];
        if (value.prototype.toString ===
             '[object Object]') {
            walkObj(value);
        } else {
             //This is not an object or is array
            console.log(value);
        }

        key_ct--;
    }
}

walkObj(JSONObject);
Cmaddux
  • 66
  • 3