I am created a web service and it returns json data as follows. [{"lat":"6.8658","lng":"79.8744"}]
But I actually need like following. [{"lat":6.8658,"lng":79.8744}]
My php code is as follows
$location_data= $this->db->query("SELECT lat,lng FROM jobs WHERE job_code='JO34656'")->result_array();
echo json_encode($location_data);
Just cast all elements into floatval
:
$location_data = $this->db->query("SELECT lat,lng FROM jobs WHERE job_code='JO34656'")->result_array();
$data = json_encode(array_map(function($e){
return array_map('floatval', $e);
}, $location_data));
Or just pick whichever column you want casted:
$location_data = json_encode(array_map(function($e){
foreach($e as $key => &$loc) {
if(in_array($key, array('lat', 'lng'))) {
$loc = floatval($loc);
}
}
return $e;
}, $location_data));
You can create a view model which converts values on creation.
Use the str.replace("Original", "Replacement");
syntax perhaps?
var test = "\"TEST\"";
test.replace(/\"/g, ""));
"TEST"
-> TEST
Use JSON_NUMERIC_CHECK
, That encodes numeric strings
as numbers
.
This option was added from 5.3.3.
$location_data= $this->db->query("SELECT lat,lng FROM jobs WHERE job_code='JO34656'")->result_array();
echo json_encode($location_data, JSON_NUMERIC_CHECK); // [{"lat":6.8658,"lng":79.8744}]
More about: JSON_NUMERIC_CHECK