Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

JSON in JQuery

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 0.68 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3.  ...
  4.  $json =json_encode($array);
  5.  
  6.  
  7. ?>
  8.        
  9. [{"name":"STA","distance":"250","code":25},
  10.  {"name":"GIS","distance":"500","code":45}]
  11.        
  12. $.getJSON("load.php", function(json){
  13.      // access object
  14.     var a=json;
  15.     pass(a);
  16.     });
  17.        
  18. var myjson={};
  19.  
  20. function pass(a){
  21.  
  22.  myjson=a;
  23.  
  24.  //Here How to get name,and distance,code
  25.  //I have Tried alert(myjson.name) returns undefined
  26. }
  27.        
  28. for(var x = 0; x < myjson.length; x++) {
  29.     alert(myjson[x].name);
  30.     // myjson[x].distance, myjson[x].code also available
  31. }
  32.        
  33. jQuery.each(myjson, function(x) {
  34.     alert(myjson[x].name);
  35. });
  36.        
  37. function pass(a) {
  38.    var i;
  39.     while(i = a.pop()) {
  40.         alert(i.name + " " + i.distance);
  41.     }
  42. }