在我的开发服务器上,来自Laravel 5控制器的JSON响应以正确的类型显示数据.

e.g

imdb_rating: 7.6
imdb_votes: 6271

But on the production server, the JSON response is sent back as strings.

imdb_rating: "7.60"
imdb_votes: "6271"

Both development and production have the same version of PHP installed (5.6.11-1).

你知道是什么导致了这种行为吗?

推荐答案

请确保使用支持本机数据类型的MySQL Native Driver.

It is possible to convert integer and float columns back to PHP numbers by setting the MYSQLI_OPT_INT_AND_FLOAT_NATIVE connection option, if using the mysqlnd library. If set, the mysqlnd library will check the result set meta data column types and convert numeric SQL columns to PHP numbers, if the PHP data type value range allows for it. This way, for example, SQL INT columns are returned as integers.

MySQL客户端库将以字符串形式返回所有字段.

Another solution would be to use json_encode options with JSON_NUMERIC_CHECK.

For example:

return response()->json(["foo" => "bar"], 200, [], JSON_NUMERIC_CHECK);

Json相关问答推荐

Jolt转换,如果任何字段为空,则将对象值设置为空

Jolt Transformation—如果子对象为空,则将父对象更新为空

如何使用ChoETL将复杂的JSON转换为CSV

当console.log返回TRUE值时,解析的JSON中的字段未定义

Flutter -控制器问题-JSON API

如果主对象中已存在属性,则不应在Jolt中引用次对象

使用 jq 如何更改键的值?

如何按键过滤

VBA-JSON 嵌套集合解析为 Excel

如何在 powerapps 中将详细信息表单转换为 json?

流编写器未写入 webapi 中的 JSON 文件

将文本转换为 python 列表

使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

使用 JSON 的 javascript 深拷贝

Spring MVC控制器中的JSON参数

如何向从文件中检索的 JSON 数据添加键值?

JSON 使用 simplejson 序列化 Django 模型

你如何在 Arrays of Arrays 上 OPENJSON

雅虎财经全币种报价 API 文档

从动态 json 数据更新力有向图上的链接