数据库中的 JSON 编码浮点数是否应该用引号引起来?
我有以下代码:
$stmt = $db->prepare("SELECT LATITUDE, LONGITUDE FROM NODEGEOLOCATION WHERE NODEID = $i");
$stmt->execute();
$path[] = $stmt->fetch(PDO::FETCH_ASSOC);
并且我使用 json_encode 将其转换为 JSON 格式。但是,我收到以下 JSON:
[{"route":[{"LATITUDE":"32.224519","LONGITUDE":"-110.947325"},{"LATITUDE":"32.227820","LONGITUDE":"-110.947293"},{"LATITUDE":"32.227843","LONGITUDE":"-110.943865"},{"LATITUDE":"32.230618","LONGITUDE":"-110.943919"},{"LATITUDE":"32.231755","LONGITUDE":"-110.943927"},{"LATITUDE":"32.233836","LONGITUDE":"-110.943963"},{"LATITUDE":"32.233850","LONGITUDE":"-110.946061"},{"LATITUDE":"32.236035","LONGITUDE":"-110.946061"},{"LATITUDE":"32.235993","LONGITUDE":"-110.948083"},{"LATITUDE":"32.235977","LONGITUDE":"-110.952433"}]
我不希望 LATITUDE 和 LONGITUDE 的值括在引号中。这可能吗?
I have the following code:
$stmt = $db->prepare("SELECT LATITUDE, LONGITUDE FROM NODEGEOLOCATION WHERE NODEID = $i");
$stmt->execute();
$path[] = $stmt->fetch(PDO::FETCH_ASSOC);
and I use json_encode to convert this to a JSON format. However, I am getting the following JSON:
[{"route":[{"LATITUDE":"32.224519","LONGITUDE":"-110.947325"},{"LATITUDE":"32.227820","LONGITUDE":"-110.947293"},{"LATITUDE":"32.227843","LONGITUDE":"-110.943865"},{"LATITUDE":"32.230618","LONGITUDE":"-110.943919"},{"LATITUDE":"32.231755","LONGITUDE":"-110.943927"},{"LATITUDE":"32.233836","LONGITUDE":"-110.943963"},{"LATITUDE":"32.233850","LONGITUDE":"-110.946061"},{"LATITUDE":"32.236035","LONGITUDE":"-110.946061"},{"LATITUDE":"32.235993","LONGITUDE":"-110.948083"},{"LATITUDE":"32.235977","LONGITUDE":"-110.952433"}]
I don't want the value of LATITUDE and LONGITUDE to be enclosed in quotes. Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 PDO 总是 以字符串形式返回数据库结果,即使它们是数字或浮点数。
PHP 的 JSON 编码器不执行任何类型嗅探:如果值保存在 PHP 字符串中,则会将其放在引号中,即使它可以表示为整数或浮点数。
您可以通过两种方式处理这个问题。
下面是一个基于
getColumnMeta
自动转换的示例。首先,我们创建一个虚拟数据库并插入一些数据。现在,我们将在演示中证明数据存在:
让我们看看 PDO 将告诉我们有关第一列的信息。
美丽的。我也对第 1 列和第 2 列执行了此操作,分别返回“double”和“string”。现在让我们构建一个列名映射来输入:
是的,它是相反的,没关系!现在让我们转换行。
田田!您可能想要用一种方便的方法来包装它,每次都手动完成有点麻烦。我还可以使用 columnCount 来获取结果集而不是计数
$row
。杀伤力大吗?这是一个未经测试的示例,它针对特定的列名称并在此过程中手动构建哈希数组。
It looks like PDO always returns database results as strings, even when they are numbers or floats.
PHP's JSON encoder doesn't perform any variety of type sniffing: if the value is held in a PHP string, it gets put in quotes, even if it could be represented as an integer or float.
You can deal with this in two ways.
json_encode
.Here's an example of automatic casting based on
getColumnMeta
. First let's create a dummy database and insert some data.Now we'll prove that the data is there, for our demo:
Let's see what PDO will tell us about the first column.
Beautiful. I did this for columns 1 and 2 as well, which returned "double" and "string" respectively. Now let's build a map of column name to type:
Yeah, it's in reverse, doesn't matter! Now let's convert our row.
Tada! You're probably going to want to wrap this in a convenience method, it's a bit hairy to do every single time by hand. I could also have used columnCount to get the number of columns in the result set rather than counting
$row
.Overkill much? Here's an untested example that targets the specific column names and builds the array of hashes by hand in the process.
我相信这取决于它们在数据库中的类型。
如果它们是数据库中的 varchar 或 string,则它们将被视为字符串。
如果它们是
FLOAT
的DOUBLE
的,则它们不应该有引号。It depends on their type in the database I believe.
If they are varchars or string in the database they are treated as strings.
If they are
FLOAT
'sDOUBLE
's, they shouldn't have quotes.