Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery - PHP JSON encode output number as string

I am trying to output a JSON string using PHP and MySQL but the latitude and longitude is outputting as a string with quotes around the values. This causes an issue when I am trying to add the markers to a google map.

Here is my code:

$sql = mysql_query('SELECT * FROM markers WHERE address !=""');
$results = array();
while($row = mysql_fetch_array($sql))
{
   $results[] = array(
      'latitude' =>$row['lat'],
      'longitude' => $row['lng'],
      'address' => $row['address'],
      'project_ID' => $row['project_ID'],
      'marker_id' => $row['marker_id']
   );
}
$json = json_encode($results);

echo "{"markers":";
echo $json;
echo "}";

Here is the expected output:

{"markers":[{"latitude":0.000000,"longitude":0.000000,"address":"2234 2nd Ave, Seattle, WA","project_ID":"7","marker_id":"21"}]}

Here is the output that I am getting:

{"markers":[{"latitude":"0.000000","longitude":"0.000000","address":"2234 2nd Ave, Seattle, WA","project_ID":"7","marker_id":"21"}]}

Notice the quotes around the latitude and longitude values.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could also try

echo json_encode( $results, JSON_NUMERIC_CHECK );

Some reference:

PHP JSON constants


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...