user813813 user813813 - 4 months ago 12
Javascript Question

Removing characters from a JSON value

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. I want to remove the " character from the values of the JSON. As the output I am currently getting is

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


What I want is

{lat: 53.399793333333, lng: -6.3844516666667}


Is there a function to do this?

Answer

Try using parseFloat:

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