博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP Returning Numeric Values in JSON
阅读量:6425 次
发布时间:2019-06-23

本文共 1411 字,大约阅读时间需要 4 分钟。

hot3.png

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.

转载于:https://my.oschina.net/netmouse/blog/411662

你可能感兴趣的文章
代码19
查看>>
Win10系列:UWP界面布局进阶5
查看>>
ABP Zero 本地化语言的初始化和扩展
查看>>
转Hibernate 一对多关联的CRUD__@ManyToOne(cascade=(CascadeType.ALL))
查看>>
FCT需求分析
查看>>
开门人和关门人(杭电1234)
查看>>
万能adapter
查看>>
开发指南专题六:JEECG微云高速开发平台代码生成
查看>>
cocos2d-x 游戏优化方案
查看>>
1.3 Quick Start中 Step 6: Setting up a multi-broker cluster官网剖析(博主推荐)
查看>>
remote desktop connection manager
查看>>
开源库RxJava、ButterKnife
查看>>
JDK内置工具jstack(Java Stack Trace)(转)
查看>>
百度之星 / 初赛第二场 B题
查看>>
Http压测工具wrk使用指南
查看>>
Excel VBA 循环“我中毒了~”
查看>>
CSS 教程Part4 [盒子模型](摘录自 W3C School)
查看>>
android开发技巧
查看>>
五个有趣的拓扑变换问题 [转]
查看>>
asp.net中的比较完美的验证码
查看>>