user813813 user813813 - 4 months ago 26
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++ ){
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?

Answer

Try using parseFloat:

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