Parsing JSON response from PHP by array index. How?

Go To StackoverFlow.com

0

I would like to know what is the latest JQuery syntax for parsing a JSON response by array index from a HTTP GET or POST request. My server side works great so I am look for way to parse the table row returned by PHP.

Here is my SERVER SIDE PHP code:

  function qry_select_account($pk_account) {

  // Global variables
  Global $db_host, $db_user, $db_pass, $db_name;  

  // Connect to database server
  $dbc = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
  // Select target database
  mysql_select_db($db_name) or die(mysql_error());

  // suchislife801 <--- Selects account information
  // Run query
  $sql_qry = mysql_query("SELECT * FROM tblaccount
                         WHERE pk_account = '$pk_account'") or die(mysql_error());

  // SQL Criteria  AND acc_confirmed = 'y' AND acc_locked = 'n'

  // Fetch table row for this user
  $row = mysql_fetch_row($sql_qry);

  print json_encode($row);

  //echo 'Account: ' . $row[0];
  //echo '<br />';
  //echo 'Passowrd: ' . $row[1];
  //echo '<br />';
  //echo 'Acc Level: ' . $row[2];
  //echo '<br />';
  //echo 'Email: ' . $row[3];
  //echo '<br />';
  //echo 'Language: ' . $row[4];
  //echo '<br />';
  //echo 'Time Zone: ' . $row[5];
  //echo '<br />';
  //echo 'Signup: ' . $row[6];
  //echo '<br />';
  //echo 'Conf Code: ' . $row[7];
  //echo '<br />';
  //echo 'Confirmed: ' . $row[8];
  //echo '<br />';
  //echo 'Locked: ' . $row[9];

  mysql_free_result($sql_qry);

  // Close Connection
  mysql_close($dbc);

  }

Here is my JQuery code which is not working.

<script type="text/javascript">
$(function(){


    $('#test_btn').live('click', function() {

    $.getJSON("http://localhost/api.php?call=select_account&p0=suchislife801", function(json){
        alert("JSON Data: " + json);

    });



});


</script>
2012-04-04 19:55
by ASPiRE
What do you mean with 'is not working'. Try console.log(json) - binarious 2012-04-04 20:00
When I press the button, no alert box come up. If I use a plain text alert box, then it shows that text - ASPiRE 2012-04-04 20:01
Check Javascript errors with Firebug or Google Chrome Dev Tools - binarious 2012-04-04 20:02
Damn it buds! I was missing a bracket.... ---> }) - ASPiRE 2012-04-04 20:12
$.each(json, function(index, value) { alert(index + ': ' + value); }) - binarious 2012-04-04 20:25


-1

You can use jQuery.parseJSON( json ) function. Make sure that json returned from server is a valid json

You can validate json response from here

Use following to check if error is returned by server

 $.getJSON("example.json", function(data) {
  alert(date);
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
);

looping through json depends on type of json

if its a map type structure, like following

var map = { 
  "data1": "value1", 
  "data2": "value2" 
}; 

you can use following to iterate through

$.each(map, function(key, value) { 
  alert(key + ': ' + value); 
});

Reference: http://api.jquery.com/jQuery.each/

2012-04-04 20:03
by Heer Makwana
getJSON doesn't need parseJSO - binarious 2012-04-04 20:05
yes. but that way we can check on if invalid json is returned by server - Heer Makwana 2012-04-04 20:12
Ads