简体   繁体   中英

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.

// 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]);
}

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]);
}
for(var i in response.DATA){
    alert(response.DATA[i];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM