0

I am pulling JSON data from a webserver using the following PHP code

 $result = mysql_query("select lat,lng from gpsdata limit 10");
 $rows = array();
 while($r = mysql_fetch_assoc($result)) {
  $rows[] = $r;
 }

 print json_encode($rows);

I am using Javascript to get this data with this

 $.getJSON('returngps.php').done(function(data) {
    for (var i=0;i< data.length;i++ ){
    console.log(data[i]);
    }
  }

My issue is the data I am getting returned. The output I am currently getting is:

   {lat: "53.399793333333", lng: "-6.3844516666667"}

What I want to work with is:

   {lat: 53.399793333333, lng: -6.3844516666667}

Is there a way to convert this?

JAAulde
  • 19,250
  • 5
  • 52
  • 63
user813813
  • 323
  • 1
  • 8
  • 28

2 Answers2

2

The problem is that the retrieval of the numbers from MySQL to PHP results in them being cast as strings. (I HATE that the relationship between PHP and MySQL doesn't respect type!) The JSON serialization, then, appropriately maintains the string type.

As such, you need to cast the values to floats while retrieving. This will result in the JSON serialization treating the value as a number instead of a string, and won't require any ridiculous string manipulations, or assumptions about data type, in your receiving JS.

$result = mysql_query("select lat,lng from gpsdata limit 10");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
    $r['lat'] = (float) $r['lat'];
    $r['lon'] = (float) $r['lon'];

    $rows[] = $r;
}
JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • but @JAAulde don't you think, it will increase API response time, as looping over a big array can be hanging. – Vishal Aug 02 '16 at 15:59
  • 1
    @VishalGupta I hear what you're saying, but in this case, absolutely not--type casting 2 values on a million rows still won't take that long. I'm sure many other areas of performance would need to be addressed before this one. I strongly believe that, in this case, since MySQL/PHP don't respect type when working together, that PHP must become authoritative for type. Also, note that _something_ is going to have to perform the conversion. Either PHP takes the hit, or JS... – JAAulde Aug 02 '16 at 16:02
  • Or you could use `CAST(lat AS DECIMAL(15,13))` in the query to get MYSQL to do all the data conversion, assuming it is stored in that database as a text or varchar of some sort – RiggsFolly Aug 02 '16 at 16:07
  • @RiggsFolly would that cause it to show up in PHP as a float/decimal? If so, that is highly preferable! In my experience, MySQL/PHP always interact such that PHP gets strings, regardless of what's in the query. :( – JAAulde Aug 02 '16 at 16:08
  • 1
    @JAAulde Not absolutely positive, would need to test it – RiggsFolly Aug 02 '16 at 16:09
  • I am still not absolutely positive OP is not just getting confused between what he see's being trancfered over th wire and what jQuery should have done to the data i.e. convert it into a javascript object where `lat` and `lng` are in fact numbers – RiggsFolly Aug 02 '16 at 16:11
1

Try using parseFloat:

data[i].lat = parseFloat(data[i].lat);
data[i].lng = parseFloat(data[i].lng);
LeDoc
  • 935
  • 2
  • 12
  • 24
  • 1
    I recommend against this solution as it requires the receiving code to make assumptions about data from a remote source. – JAAulde Aug 02 '16 at 15:57