为什么JSON模块引用一些数字而不引用其他数字? [英] Why does the JSON module quote some numbers but not others?

查看:110
本文介绍了为什么JSON模块引用一些数字而不引用其他数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近切换到了新的JSON2 perl模块. 我以为现在所有的东西都返回报价了. 但是我遇到了一些情况,在perl创建的json字符串中,一个数字(250)作为未引用的数字返回.

We recently switched to the new JSON2 perl module. I thought all and everything gets returned quoted now. But i encountered some cases in which a number (250) got returned as unquoted number in the json string created by perl.

出于好奇: 有人知道为什么存在这种情况以及json模块如何决定是否引用值吗?

推荐答案

您可以通过执行以下操作将其强制为字符串:

You can force it into a string by doing something like this:

$number_str = '' . $number;

例如:

perl -MJSON -le 'print encode_json({foo=>123, bar=>"".123})'
{"bar":"123","foo":123}

看起来旧版JSON具有自动转换可以设置的功能.您没有将$JSON::AUTOCONVERT设置为真实值吗?

It looks like older versions of JSON has autoconvert functionality that can be set. Did you not have $JSON::AUTOCONVERT set to a true value?

这篇关于为什么JSON模块引用一些数字而不引用其他数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆