Yannick
Yannick

Reputation: 3653

Read JSON response in JQuery

This is my json response

{"COLUMNS":["LABEL1", "LABEL2", "RATE1", "RATE2"], "DATA":[["tps", "tvq", 10.0, 20.0]]} 

And I want to be able to loop only over the DATA element.

I don't have any code because I have no clue how to do this. Just learning this new language.

Upvotes: 0

Views: 80

Answers (3)

Shef
Shef

Reputation: 45589

If there are only arrays inside the DATA and you need each array, then do it like this:

for(var i in response.DATA){
    alert(response.DATA[i]);
}

Otherwise if you want all the values tps, tvq, 10.0, and 20.0, then do it like this:

for(var i in response.DATA[0]){
    alert(response.DATA[0][i]);
}

Update

var arrData = response.DATA[0],
    sizeOfData = arrData.length,
    i = 0;

for(i; i < sizeOfData; i++){
    alert(arrData[i]);
}

Upvotes: 0

Raul Agrait
Raul Agrait

Reputation: 6018

// response.DATA is an array with just one element
var dataElements = response.DATA;

// The first element in that array is another array containing your data
var firstData = dataElements[0];

// Loop through and access each individual element
for (var i = 0; i < firstData.length; i++) {
  alert(firstData[i]);
}

Upvotes: 2

DrStrangeLove
DrStrangeLove

Reputation: 11557

for(var i in response.DATA){
    alert(response.DATA[i];
}

Upvotes: 0

Related Questions