Jquery Json dynamic variable name generation
- by PlanetUnknown
I make a jquery .ajax call and I'm expecting a json result.
The catch is, if there are say 5 authors, I'll get author_details_0, author_details_1, author_details_2, etc.... How can I dynamically construct the name of the variable to retrieve from json ? I don't know how many authors I'll get, there could be hundreds.
$.ajax({
type: "POST",
url: "/authordetails/show_my_details/",
data: af_pTempString,
dataType: "json",
beforeSend: function() {
},
success: function(jsonData) {
console.log("Incoming from backend : " + jsonData.toSource());
if(jsonData.AuthorCount)
{
console.log("Number of Authors : " + jsonData.AuthorCount);
for (i = 0; i < jsonData.AuthorCount; i++)
{
temp = 'author_details_' + i; <-------------------This is the name of the variable I'm expecting.
console.log("Farm information : " + eval(jsonData.temp) ); <----- This doesn't work, how can I get jsonData.author_details_2 for example, 'coz I don't know how many authors are there, there could be hundreds.
}
}
Please let me know if you have any idea how to solve this !
Much appreciated.