
这就是现在生成JsON的方式:
my $Json = JsON->new->allow_nonref->allow_unkNown->allow_blessed->utf8; $output = $Json->encode($hash);
什么是一个很好的方式来说,“并引用该$hash中的每个标量”?
解决方法 JsON的两个后端(JsON :: PP和JsON :: XS)都将输出类型基于值的内部存储.解决方案是在数据结构中对非参考标量进行字符串化.sub recursive_inplace_stringification { my $reftype = ref($_[0]); if (!length($reftype)) { $_[0] = "$_[0]" if defined($_[0]); } elsif ($reftype eq 'ARRAY') { recursive_inplace_stringification($_) for @{ $_[0] }; } elsif ($reftype eq 'HASH') { recursive_inplace_stringification($_) for values %{ $_[0] }; } else { dIE("Unsupported reference to $reftype\n"); }}# Convert numbers to strings.recursive_inplace_stringification($hash);# Convert to JsON.my $Json = JsON->new->allow_nonref->utf8->encode($hash); 如果您确实需要allow_unkNown和allow_blessed提供的功能,则需要在recursive_inplace_stringification中重新实现它(如果许可允许,可以通过从JsON :: PP复制它),或者在调用recursive_inplace_stringification之前可以使用以下命令:
# Convert objects to strings.$hash = JsON->new->allow_nonref->decode( JsON->new->allow_nonref->allow_unkNown->allow_blessed->encode( $hash));总结
以上是内存溢出为你收集整理的Perl JSON将所有数字视为字符串全部内容,希望文章能够帮你解决Perl JSON将所有数字视为字符串所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)