简体   繁体   English

PHP json_decode返回错误代码4

[英]PHP json_decode return error code 4

I had previously asked the same question. 我之前曾问过同样的问题。 I would like to decode the json from: http://pad.skyozora.com/data/pets.json . 我想解读json: http//pad.skyozora.com/data/pets.json Below is the code I used previously: 以下是我之前使用的代码:

<?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. 从最后一个答案我知道json返回中有UTF8 DOM。 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_decode在webservice调用后返回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. 这成功地将json解码为数组。 However all Chinese and Japanese character string were removed too. 但是所有中文和日文字符串也被删除了。 Any ideas? 有任何想法吗?

Edited: 编辑:

I used http://jsonlint.com/ to decode the json from http://pad.skyozora.com/data/pets.json . 我使用http://jsonlint.com/http://pad.skyozora.com/data/pets.json解码json。 It stops at here: 它停在这里:

[
            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有什么问题。

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: 如果您在浏览器中转到该网址,则可以看到以下消息:

bad character in string literal at line 1 column 294388 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM