dszm02606009 2017-03-09 14:14
浏览 104

推测PHP版本的差异导致JSON输出的差异 - 整数引用

I have a testing server with PHP version 5.5.9, and we have a QA server with PHP version 5.6.16.

I am for the sake of this post using the same code branch on both instances (full disclosure: it's a Laravel install but I'm not sure this is a Laravel issue)

On the QA server, an API returns JSON with the integers unquoted as follows (this is simplified):

{["id":1,"name":"John"],["id":2,"name":"Sam"]}

But on my server (the 5.6.16 one), the same exact API call returns:

{["id":"1","name":"John"],["id":"2","name":"Sam"]}

And, Angular is not liking that and does not parse it.

It's not the data

The data and tables the data is coming from are exactly alike in structure - I have verified this.

I don't want to "fix" Angular

Though in theory Angular should handle this(?), I don't want to touch our coding for the front end.

I want the output to be the same..

Regardless of what Angular is doing, the obvious solution is to have the outputs be exactly equal.

What do I do?

  • I want to avoid upgrading PHP on my server - that might open up more problems
  • I am thinking that there is a setting in json_encode() which can be set to change this but I do not know which one.
  • Also there may be a setting in laravel itself which could be at least locally changed on my end. For the record there are well over 100+ json_encode calls in the codebase.
  • 写回答

2条回答 默认 最新

  • dpzzkfb1244 2017-03-09 14:31
    关注

    Its not an issue in json_encode() it is what you are encoding that makes a difference. See this simple example

    $a = array(
        'id' => 111,
        'astring' => 'test',
        'OhLookItsANumberRecordedAsAString' => '456'
    );
    $json = json_encode($a);
    echo $json;
    

    Result

    {"id":111,
     "astring":"test",
     "OhLookItsANumberRecordedAsAString":"456"
    }
    

    But if you make sure the integers are integers all is as you expect

    $a = array(
            'id' => 111,
            'astring' => 'test',
            'OhLookItsANumberRecordedAsAString' => '456',
            'ANumber' => (int)'456'
        );
    $json = json_encode($a);
    echo $json;
    

    Result

    {"id":111,
     "astring":"test",
     "OhLookItsANumberRecordedAsAString":"456",
     "ANumber":456
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c