PHP Returning Numeric Values in JSON
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP Returning Numeric Values in JSON,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1454字,纯文字阅读大概需要3分钟。
内容图文
![PHP Returning Numeric Values in JSON](/upload/InfoBanner/zyjiaocheng/1187/e2a5c3879af54978a412ae90df80f3c8.jpg)
When I wrote about launching a prototype of a new joind.in API, quite a few people started to try it out. My friend David Soria Parra emailed me to point out that many of the numbers in the API were being returned as strings. He said:
It‘s just a standard problem of PHP REST services. When I try to access it with java I have to convert it over and over again to ints.
I did have a quick look at the PHP manual page for json_encode but I didn‘t see anything mentioning this. A few weeks later (my inbox is a black hole and it takes a while to process these things) I fell over a throwaway comment to an undocumented constant JSON_NUMERIC_CHECK, and I added the constant name to my todo list. In the time it took for me to actually get around to googling for this, some wonderful person updated the PHP manual page (this is why I love PHP) to include it as a documented option, and someone else had added a user contributed note about using it.
It turns out, this constant does exactly what I need. Here‘s a simple use case:
echo json_encode(array(‘event_id‘ => ‘603‘)); echo json_encode(array(‘event_id‘ => ‘603‘), JSON_NUMERIC_CHECK);
and the output:
{"event_id":"603"} {"event_id":603}
There are probably some situations in which you don‘t want all your looks-like-a-number data to be returned as a number, but for now it seems to be a good fit for api.joind.in.
原文:http://my.oschina.net/netmouse/blog/411662
内容总结
以上是互联网集市为您收集整理的PHP Returning Numeric Values in JSON全部内容,希望文章能够帮你解决PHP Returning Numeric Values in JSON所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。