Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

json - 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. 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. I tried the answer from a similar question: json_decode returns NULL after webservice call, but all of the answers not work for me.

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-x1Fx80-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?

Edited:

I used http://jsonlint.com/ to decode the json from http://pad.skyozora.com/data/pets.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The return error code 4 is due to a JSON_ERROR_SYNTAX you must try to fix your 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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...