views:

1024

answers:

4

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.

+1  A: 

Solved it !

Instead of using "temp" - temp = 'author_details_' + i; and then doing - eval(jsonData.temp)

Just this works - eval("jsonData.author_details_" + i)

Since the data is from the database, it is safe, since I put everything in database after checking. Hence eval will not pose a security threat.

If you have any more solutions, I'd be glad to hear them !

PlanetUnknown
+1  A: 

You can access object properties in two ways. First is dot notation, as in object.property. You can also use bracket notation, as in object['property']. No need for eval here:

 var propName = 'author_details_' + i;
 alert('Details for author ' + i + ' = ' + jsonData[propName];

This page addresses your assumption at the bottom.

Barnabas Kendall
That works too ! Thanks Barnabas, I learned something here !
PlanetUnknown
A: 

why not store the json as an array? instead of:

{
    "author_details_1":
    {
     ...
    },
    "author_details_2":
    {
     ...
    },
    "author_details_3":
    {
     ...
    },
    "author_details_4":
    {
     ...
    }
    ...
}

try

{
    "author_details":
    [
     {
      ...
     },
     {
      ...
     },
     {
      ...
     },
     {
      ...
     }
    ]
    ...
}

Then you can access it with author_details[i].

Eric
Thanks Eric. Let me try that as well.
PlanetUnknown
A: 

Got the problem with jQuery and $.ajax function. Solved by defining the 'data' object outside of the $.ajax function :

var data = new Object();
data.ContentNodeID = button.siblings('.ContentNodeID').val();
data.ContentObjectID = button.siblings('.ContentObjectID').val();
data.ActionAddToBasket = '';
data['eZOption['+$('[name=printFormatAttributeId]').val()+'][]'] = $('.eZOption').val();

$.ajax({
    cache: false,
    type: 'POST',
    url: button.parent().attr('action'),
    data: data,
    success: function(data){
                 button.siblings('.addtobasket_success').css('display', 'inline');
                 $('#shop_button').removeClass('disabled');
                 $('#shop_button').removeAttr('disabled');
             },
    error: function(data) {
                 button.siblings('.addtobasket_failure').css('display', 'inline');
            }
});
Franck