PHP json_decode返回错误代码4 [英] PHP json_decode return error code 4

查看:433
本文介绍了PHP json_decode返回错误代码4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前曾问过同样的问题。我想从以下位置解码json: http://pad.skyozora.com/data/pets。 json 。下面是我以前使用的代码:

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

从最后一个答案中我知道json返回中存在UTF8 DOM。我尝试了一个类似问题的答案: json_decode在网络服务调用后返回NULL ,但所有答案对我都不起作用。

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-\x1F\x80-\xFF]/', '', $html);
var_dump(json_decode($html, true));
var_dump(json_last_error());
?>

这成功地将json解码为数组。但是,所有的中文和日文字符串也被删除。有任何想法吗?

This successfully decode the json into array. However all Chinese and Japanese character string were removed too. Any ideas?

已编辑

我使用了 http://jsonlint.com/ 解码来自 http://pad.skyozora.com/data/pets.json 。它停在这里:

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",
        [
            "ウルカヌ火山",
            "メジェド

并给我错误:

Parse error on line 5001:
...山",                "メジェド
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

但是我看不到json的这一部分有任何问题。

But I couldn't see any problem with this part of json.

解决方案

返回错误代码4是由于 JSON_ERROR_SYNTAX 引起的,您必须尝试修复json。

The return error code 4 is due to a JSON_ERROR_SYNTAX you must try to fix your json.

如果您在浏览器中转到URL,您会看到以下消息:

If you go to the url in a browser you can see this message:

bad character in string literal at line 1 column 294388 

这篇关于PHP json_decode返回错误代码4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆