user813813 user813813 - 2 months ago 8x
Javascript Question

How to handle JSON that contains numeric strings

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++ ){

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?


Try using parseFloat:

data[i].lat = parseFloat(data[i].lat);
data[i].lng = parseFloat(data[i].lng);