<code class="lang-php">$json_data=file_get_contents('http://www.btc38.com/trade/getTradeList.php?coinname=XRP'); $data=json_decode($json_data,true); var_dump($data);//为什么仍然输出字符串???? </code>
ps:在jslint.com 测试$json_data为valid
<code class="lang-php">$json_data=file_get_contents('http://www.btc38.com/trade/getTradeList.php?coinname=XRP'); $data=json_decode($json_data,true); var_dump($data);//为什么仍然输出字符串???? </code>
ps:在jslint.com 测试$json_data为valid
BOM!
我直接 echo 从网页获取到的内容,然后传递给 json_pp:
<code class="lang-sh">>>> php a.php | json_pp malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "\x{ef}\x{bb}\x{bf}{"...") at /usr/bin/core_perl/json_pp line 44. </code>
<code><?php $header = array( "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36" ); $curl = curl_init('http://www.btc38.com/trade/getTradeList.php?coinname=XRP'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); $res = curl_exec($curl); curl_close($curl); $arr = json_decode($res); switch (json_last_error()) { case JSON_ERROR_NONE: echo '没有错误发生'; break; case JSON_ERROR_DEPTH: echo '到达了最大堆栈深度'; break; case JSON_ERROR_STATE_MISMATCH: echo '无效或异常的 JSON'; break; case JSON_ERROR_CTRL_CHAR: echo '控制字符错误,可能是编码不对'; break; case JSON_ERROR_SYNTAX: echo '语法错误'; break; case JSON_ERROR_UTF8: echo '异常的 UTF-8 字符,也许是因为不正确的编码。'; break; default: echo '未知错误'; break; } ?> </code>
确实挺奇怪的,运行如上代码,使用json_last_error()
最终提示的是语法错误
。根据错误关键词搜索到SO中的这个问题:json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK 情况比较类似。按照答案之一说的怀疑了一下编码问题,不过看了下是UTF-8,没有问题。
单纯的复制页面中的字符串的话是没有问题的(复制到页面和PHP中都没有问题)。这个不算是答案,算是帮助题主补充说明吧,我也还在努力的寻找答案中。
感谢 @依云 的答案,让我明白了原来是可恶的BOM的问题,关于BOM的问题 @依云 的维基百科链接已经很详细了,我就不多说了,这里就说一下怎么去除吧。其实BOM就是在字符串的最开头增加了三个字符,我们把它去除掉就好了。在json_decode
之前用substr
去除就好了,例如:
<code>$res = substr($res, 3); $arr = json_decode($res, true); </code>
我这里没有问题啊:
<code>{"buyOrder":[{"price":"0.222000","amount":"63077.877631"},{"price":"0.221000","amount":"82921.415688"},{"price":"0.220000","amount":"53624.458032"},{"price":"0.219000","amount":"120232.956415"},{"price":"0.218000","amount":"58609.097966"},{"price":"0.217000","amount":"48330.724883"},{"price":"0.216000","amount":"15955.233203"},{"price":"0.215000","amount":"146085.153646"},{"price":"0.214000","amount":"17075.000000"},{"price":"0.213000","amount":"18729.000000"}], "sellOrder":[{"price":"0.223000","amount":"10724.828662"},{"price":"0.224000","amount":"133336.648858"},{"price":"0.225000","amount":"140519.661232"},{"price":"0.226000","amount":"10095.282427"},{"price":"0.227000","amount":"26586.879929"},{"price":"0.228000","amount":"27247.504336"},{"price":"0.229000","amount":"35233.324750"},{"price":"0.230000","amount":"84477.240158"},{"price":"0.231000","amount":"17260.931218"},{"price":"0.232000","amount":"72333.275935"}], "trade":[{"price":"0.223000","volume":"52.322237","time":"2013-12-12 18:41:21","type":"1"},{"price":"0.222000","volume":"1.000000","time":"2013-12-12 18:41:02","type":"2"},{"price":"0.223000","volume":"849.956784","time":"2013-12-12 18:37:45","type":"1"},{"price":"0.223000","volume":"8181.433350","time":"2013-12-12 18:37:45","type":"1"},{"price":"0.223000","volume":"1696.845442","time":"2013-12-12 18:34:42","type":"1"},{"price":"0.223000","volume":"63.826185","time":"2013-12-12 18:34:42","type":"1"},{"price":"0.222000","volume":"322.039509","time":"2013-12-12 18:33:57","type":"2"},{"price":"0.222000","volume":"676.960491","time":"2013-12-12 18:33:57","type":"2"},{"price":"0.222000","volume":"3628.741136","time":"2013-12-12 18:33:40","type":"2"},{"price":"0.223000","volume":"0.672054","time":"2013-12-12 18:33:31","type":"1"},{"price":"0.222000","volume":"1.000000","time":"2013-12-12 18:33:11","type":"2"},{"price":"0.223000","volume":"442.969488","time":"2013-12-12 18:32:41","type":"1"},{"price":"0.222000","volume":"2.455251","time":"2013-12-12 18:31:49","type":"2"},{"price":"0.223000","volume":"20.715520","time":"2013-12-12 18:28:48","type":"1"},{"price":"0.223000","volume":"465.314511","time":"2013-12-12 18:26:54","type":"1"},{"price":"0.223000","volume":"6.502242","time":"2013-12-12 18:26:50","type":"1"},{"price":"0.224000","volume":"35.229334","time":"2013-12-12 18:22:31","type":"1"},{"price":"0.224000","volume":"100.000000","time":"2013-12-12 18:22:13","type":"1"},{"price":"0.223000","volume":"1609.883424","time":"2013-12-12 18:22:07","type":"2"},{"price":"0.223000","volume":"99.900000","time":"2013-12-12 18:21:35","type":"2"}]} </code>
保存到json_file
然后
<code class="lang-php">var_dump(json_decode(file_get_contents('json_file'), true)); </code>
结果是
<code>array(3) { ["buyOrder"]=> array(10) { [0]=> array(2) { ["price"]=> string(8) "0.222000" ["amount"]=> string(12) "63077.877631" } [1]=> array(2) { ["price"]=> string(8) "0.221000" ["amount"]=> string(12) "82921.415688" } 以下省略... </code>
可能是服务器不太稳定,传输的数据错误了。
求截图...