Jquery Json dynamic variable name generation

Posted by PlanetUnknown on Stack Overflow See other posts from Stack Overflow or by PlanetUnknown
Published on 2009-09-27T02:56:11Z Indexed on 2010/05/25 10:11 UTC
Read the original article Hit count: 385

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JSON