json_decode返回有效的JSON数据时返回NULL

3

这是我的JSON解析器函数中的一小段代码,它可以很好地处理大约十几个不同的JSON文件:

这些打印输出是为了当前调试目的

    $this->response = stripslashes($this->response);
    print_r($this->response);
    $this->response = json_decode($this->response);
    print_r($this->response);

第一次打印输出了JSON字符串,而第二次打印输出了null。通过使用json_last_error函数,我发现PHP拒绝解析JSON,因为它的语法无效。

这是我正在解析的JSON的片段(完整代码在这里),可以通过我在Google上找到的所有验证器进行验证:

{
"success": 1,
"stores": [
    {
        "name": "Winton",
        "address": "370-374 , Wimborne Road, Bournemouth, Dorset BH92HE",
        "telephone": "",
        "email": "info@99pstoresltd.com",
        "website": "",
        "description": "Mon  - 09.00-18.00  Tue - 09.00-18.00  Wed - 09.00-18.00  Thu - 09.00-18.00  Fri - 09.00-18.00  Sat - 09.00-18.00  Sun - 10.00-16.00",
        "lat": "50.7413",
        "lng": "-1.87926",
        "titlewebsite": "Website",
        "titleemail": "Email",
        "titletel": "Telephone",
        "titlecontactstore": "Contact this store",
        "titlekm": "km",
        "titlemiles": "miles",
        "cat_name": "",
        "cat_img": "",
        "img": ""
    },
    {
        "name": "Boscombe",
        "address": "The Sovereign Centre, Boscombe, Bournemouth, Dorset BH14SX",
        "telephone": "",
        "email": "info@99pstoresltd.com",
        "website": "",
        "description": "Mon  - 08.00-18.00  Tue - 08.00-18.00  Wed - 08.00-18.00  Thu - 08.00-18.00  Fri - 08.00-18.00  Sat - 08.00-18.00  Sun - 10.00-16.00",
        "lat": "50.7272",
        "lng": "-1.83952",
        "titlewebsite": "Website",
        "titleemail": "Email",
        "titletel": "Telephone",
        "titlecontactstore": "Contact this store",
        "titlekm": "km",
        "titlemiles": "miles",
        "cat_name": "",
        "cat_img": "",
        "img": ""
    }]
}

我不知道为什么这个JSON无法解析,因为在我的看来它看起来很好!我一直盯着它看了很长时间,所以非常希望能听到大家的想法。

编辑: 从第一个print_r中复制并粘贴数据,然后通过json_decode处理可以正常工作。我猜这意味着JSON的来源存在问题,可能会影响编码或其他方面?


不知道为什么以前我没有尝试过,但如果我使用pastebin中的数据,它也可以很好地解析。然而,这正是第一个print_r给我的相同数据(我从那个print_r复制并粘贴了那个pastebin)。这是否意味着这是某种编码问题? - jackmullen
1
我也成功解析了,你有检查可能的编码问题吗?为什么要运行 stripslashes? - Saa
@MikeBrant 服务端代码使用json_encode()向我提供JSON数据(这是我所知道的全部信息),因此在这种情况下,JSON应该是正确的格式和编码?然而,json_decode()仍然给我返回了null值。 - jackmullen
你能否只是将输出回显到HTML中进行检查?使用以下代码:echo htmlentities($this->response); 然后,将HTML源代码粘贴到jsfiddle.net中,或者将浏览器渲染的字符串粘贴到另一个pastebin中。 - Joseph Shih
@JosephShih 是的,确实非常奇怪。我甚至尝试使用 htmlentities($this->response);html_entity_decode($this->response);,但仍然从 json_decode() 得到了空值。 - jackmullen
显示剩余8条评论
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接