json_decode使用有效的JSON数据返回NULL

时间:2015-09-01 14:56:26

标签: php json validation

这是我的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(full thing here)的剪辑,它可以通过我在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源自哪里可能会弄乱编码或其他什么问题?

1 个答案:

答案 0 :(得分:0)

试试这个:

json_decode($this->response, true);

来自有关第二个参数的文档

  

当为TRUE时,返回的对象将被转换为关联数组。

来源:http://php.net/manual/en/function.json-decode.php