user1988149
user1988149

Reputation: 23

Jquery nested object in json

I'm calling some JSON (cut short for size reason);

"numExecutors" : 2,
"description" : null,
"jobs" : [
{
  "name" : "Test",
  "url" : "http://www.carlbruiners.com/job/Test/",
  "color" : "blue"
},
{
  "name" : "Test 2",
  "url" : "http://www.carlbruiners.com/job/Test%202/",
  "color" : "blue"
}
]

I need to be able to get at the jobs.name field, but I've tried various loops but I can't get it to work. My Jquery code is;

function getJobs() {
$.jsonp({
  "url": jenkins_url + "/api/json?jsonp=jsoncallback=?",
  "data": {
      "alt": "json-in-script"
  },
  "success": function(data) {
    //alert('Sucess');
    for(var key in data) {
        var value = data[key];
        //alert(value);
    }
  },
  "error": function(e) {
    alert('Failed ' + e);
  }
});
}

Upvotes: 1

Views: 143

Answers (1)

adeneo
adeneo

Reputation: 318162

jobs is an array of objects :

object.jobs[0].name

I'm guessing something like :

function getJobs() {
    $.getJSON({
        url : jenkins_url + "/api/json?jsonp=jsoncallback=?",
        data: {alt: "json-in-script" }
    }).done(function(data) {
        $.each(data.jobs, function(idx, val) {
            console.log(val);
        });
    }).fail(function(e) {
        alert('Failed ' + e);
    });
}

And it's asynchronous!

Upvotes: 2

Related Questions