I use jQuery $.getJSON to get data from API that return json data in multiple level. The one of the level is dynamic and I want to use varible to access that. Here's the example of result:
{
"status" : "200",
"result" : {
"DYNAMIC_DATA_1" : {
"name" : "Johny",
"age" : "20"
},
"DYNAMIC_DATA_2" : {
"name" : "Jenny",
"age" : "25"
}
}
}
from my example above, the "DYNAMIC_DATA_1" and "DYNAMIC_DATA_2" is actually a unique id.
Here is my code to request the json data:
$(function(){
$.getJSON('/api/username?id='+DYNAMIC_USER_ID, function(data) {
console.log(data.result.DYNAMIC_DATA.name);
});
});
I want to dynamically loop the data.result.the-id/key to get each name value.
console.log(data.result.DYNAMIC_DATA_1.name);? The keyDYNAMIC_DATAdoes not exist.