JSON String of Associated Arrays

When JSON returns a string of associated arrays, do this….


Sometimes a sql query via Ajax to a php program as below…

var stmt = "select p.columnP1, s.columnS1, c.columnC1, c.columnC2, s.columnS2 from SomeTable s inner join AnotherTable a on a.id = s.atID  inner join AThirdTable t on t.id = s.attid

…and because multiple rows are returned, JSON encoding creates a it a string of associated arrays to Javascript…

You can process it by making your jquery handler look something  like this….

$.get('getTheRow.php', {sqlStmt: stmt}, function(data)
// you can see the elements by looping through the object as follows:
  var i;
  for(i in data){
    // I usually make the first element a success indicator
    // hence, the test for i==0... remaining elements 
    // 1 thru n, where n is the size of the array 
    // are the data.
    if(i==0) {
      } else {
      // if an array element is null, detect it as follows:
      if(data[i].columnS2 == null) {
       alert ('yippee... null detected!!');
}, 'json');

Leave a Reply