PHP json_decode 返回错误代码 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29326045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP json_decode return error code 4
提问by Patrick Ng
I had previously asked the same question. I would like to decode the json from: http://pad.skyozora.com/data/pets.json. Below is the code I used previously:
我之前也问过同样的问题。我想解码来自:http: //pad.skyozora.com/data/pets.json的 json 。下面是我之前使用的代码:
<?php
$html=file_get_contents("http://pad.skyozora.com/data/pets.json");
var_dump(json_decode($html,true)); //return null
var_dump(json_last_error()); // return 4
?>
From the last answer I know there is UTF8 DOM in the json return. I tried the answer from a similar question: json_decode returns NULL after webservice call, but all of the answers not work for me.
从最后一个答案我知道json返回中有UTF8 DOM。我尝试了类似问题的答案:json_decode 在 webservice call 后返回 NULL,但所有的答案都不适合我。
And after do more research I found a way that works:
在做了更多的研究之后,我找到了一种有效的方法:
<?php
$html=file_get_contents("http://pad.skyozora.com/data/pets.json");
$html=preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $html);
var_dump(json_decode($html, true));
var_dump(json_last_error());
?>
This successfully decode the json into array. However all Chinese and Japanese character string were removed too. Any ideas?
这成功地将json解码为数组。然而,所有的中文和日文字符串也被删除了。有任何想法吗?
Edited:
编辑:
I used http://jsonlint.com/to decode the json from http://pad.skyozora.com/data/pets.json. It stops at here:
我使用http://jsonlint.com/解码来自http://pad.skyozora.com/data/pets.json的 json 。它停在这里:
[
161,
"進化的紅面具",
0,
-1,
0,
-1,
1,
1,
10,
50,
1,
0,
0,
0,
0,
[
],
[
0,
0,
0,
0,
0,
0,
0
],
"http:\/\/i.imgur.com\/Y1jZlGW.png",
[
"ウルカヌ火山",
"メジェド
and give me the error:
并给我错误:
Parse error on line 5001:
...山", "メジェド
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
But I couldn't see any problem with this part of json.
但是我看不出这部分 json 有任何问题。
回答by Adrian Cid Almaguer
The return error code 4 is due to a JSON_ERROR_SYNTAX
you must try to fix your json.
返回错误代码 4 是由于JSON_ERROR_SYNTAX
您必须尝试修复 json。
If you go to the url in a browser you can see this message:
如果您在浏览器中访问该 url,您可以看到以下消息:
bad character in string literal at line 1 column 294388