Helder Alvarez
Helder Alvarez

Reputation: 111

basic json_encode and jquery multiple values

This is my 3rd jquery script and I'm not sure what I'm doing.

I want to read multiple json values from 3 different queries. My JSON is returning what I was expecting:

key
 1    ["value1"]
 2    ["value2"]
max   2101
total 22

I can read the first two values using somethin like this:

  ajax(                                      
   url: 'api.php, data: "", dataType: 'json',  success: function(rows)        
    { 
      for (var i in rows)
      {
        var row = rows[i];
        value1 = row[0];
      }

How can I read the max value from the JSON? I tried thing like:

 $row[max], $row['max'], etc.

I'm reading the values with JavaScript

Thanks

Upvotes: 0

Views: 169

Answers (2)

the_red_baron
the_red_baron

Reputation: 888

Try using javascript's eval function in your success handler. It will convert your returned json text into a javascript object which you can then manipulate. Something like:

success: function(data) {
         var returned_array = eval(data);

         //access values as if it were a regular javascript object
         alert(returned_array['max']);
         //OR
         alert(returned_array.max);
       }

Upvotes: 0

RobIII
RobIII

Reputation: 8821

row.max might be what you're looking for.

Upvotes: 1

Related Questions