<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[个人原创]php5.4以前版本的json_encode函数不支持BIGINT的问题及解决方案]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Thu, 08 Sep 2011 03:44:40 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	php json_encode函数不支持BIGINT的问题<br/>新浪微博的32位id，twitter的id过大超过BIGINT也遇到类似的问题，发现是php的json_decode函数不支持62位的BIGINT, 溢出所致.<br/>php好像不分类型但它也依赖于CPU位数，但mysql bigint&nbsp;&nbsp;范围 -9223372036854775808 到 9223372036854775807<br/><br/>在新的php(5.4版本) json_decode里倒是有一个开关”JSON_BIGINT_AS_STRING”开关,可以使json_decode默认把BITINT当成string处理.<br/>而我们的实际情况是,更新php的版本的速度不会有那么快.而且大部分情况下,咱们也控制不了具体使用哪个php版本，只好采取一个折中<br/>办法：<br/>把微博API返回的json字符串预处理一下,然后再调用json_decode.<br/>也就是把<br/><textarea name="code" class="html" rows="15" cols="100">
&quot;id&quot;:12345678901234567890
</textarea><br/>换成string的类型:<br/><textarea name="code" class="html" rows="15" cols="100">
&quot;id&quot;:&quot;12345678901234567890&quot;
</textarea><br/>代码片段如下,希望对和我类似情况的人有用.<br/><textarea name="code" class="html" rows="15" cols="100">
if (define(&#039;JSON_BIGINT_AS_STRING&#039;)) &#123;
&nbsp;&nbsp;$json = json_decode($data, false, 512, JSON_BIGINT_AS_STRING);
&#125;else&#123;
&nbsp;&nbsp;//convert bigint to string
&nbsp;&nbsp;$data = preg_replace(&#039;/&quot;id&quot;:([0-9]+)/&#039;, &#039;&quot;id&quot;:&quot;$1&quot;&#039;, $data);
&nbsp;&nbsp;$json = json_decode($data, false);
&#125;
</textarea><br/><br/>截取部分实验了一把，如下：<br/><textarea name="code" class="html" rows="15" cols="100">
 &lt;?php
 $data=&#039;&#123;&quot;a&quot;:1,&quot;b&quot;:2,&quot;c&quot;:3,&quot;d&quot;:4,&quot;e&quot;:9223372036854775807,&quot;id&quot;:9223372036854775807&#125;&#039;;
 
&nbsp;&nbsp;&nbsp;&nbsp; $data = preg_replace(&#039;/&quot;id&quot;:([0-9]+)/&#039;, &#039;&quot;id&quot;:&quot;$1&quot;&#039;, $data);
&nbsp;&nbsp;&nbsp;&nbsp; $json = json_decode($data,false);
&nbsp;&nbsp;&nbsp;&nbsp; echo $data;
&nbsp;&nbsp;&nbsp;&nbsp; print_r($json);
 ?&gt;
</textarea><br/>运行如下：<br/><textarea name="code" class="html" rows="15" cols="100">
php encode.php
&#123;&quot;a&quot;:1,&quot;b&quot;:2,&quot;c&quot;:3,&quot;d&quot;:4,&quot;e&quot;:9223372036854775807,&quot;id&quot;:&quot;9223372036854775807&quot;&#125;stdClass Object
(
&nbsp;&nbsp;&nbsp;&nbsp;[a] =&gt; 1
&nbsp;&nbsp;&nbsp;&nbsp;[b] =&gt; 2
&nbsp;&nbsp;&nbsp;&nbsp;[c] =&gt; 3
&nbsp;&nbsp;&nbsp;&nbsp;[d] =&gt; 4
&nbsp;&nbsp;&nbsp;&nbsp;[e] =&gt; 9.22337203685E+18
&nbsp;&nbsp;&nbsp;&nbsp;[id] =&gt; 9223372036854775807
)
</textarea><br/>Json中id这个给转成了sting，输出正常，而e这个json的键值，给当成了浮点数，输出为：<br/><textarea name="code" class="html" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;[e] =&gt; 9.22337203685E+18
</textarea><br/>通过var_dump查其类型分别如下：<br/><textarea name="code" class="html" rows="15" cols="100">
 [&quot;e&quot;]=&gt;
 float(9.22337203685E+18)
 [&quot;id&quot;]=&gt;
 string(19) &quot;9223372036854775807&quot;
</textarea><br/><br/>参考：http://drupal.org/node/985544
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [个人原创]php5.4以前版本的json_encode函数不支持BIGINT的问题及解决方案]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>