当前位置:Gxlcms > PHP教程 > json_decode一个json字符为什么还是字符

json_decode一个json字符为什么还是字符

时间:2021-07-01 10:21:17 帮助过:35人阅读

$json_data=file_get_contents('http://www.btc38.com/trade/getTradeList.php?coinname=XRP');
$data=json_decode($json_data,true);
var_dump($data);//为什么仍然
输出字符串????

ps:在jslint.com 测试$json_data为valid

回复内容:

$json_data=file_get_contents('http://www.btc38.com/trade/getTradeList.php?coinname=XRP');
$data=json_decode($json_data,true);
var_dump($data);//为什么仍然
输出字符串????

ps:在jslint.com 测试$json_data为valid

BOM!

我直接 echo 从网页获取到的内容,然后传递给 json_pp:

>>> 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.


确实挺奇怪的,运行如上代码,使用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去除就好了,例如:

$res = substr($res, 3);
$arr = json_decode($res, true);

我这里没有问题啊:

{"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"}]}

保存到json_file

然后

var_dump(json_decode(file_get_contents('json_file'), true));

结果是

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"
    }
以下省略...

可能是服务器不太稳定,传输的数据错误了。

求截图...

人气教程排行