简体   繁体   中英

json_decode returning NULL with valid JSON data

This is a snippet of code inside my JSON parser function, which is working fine with about a dozen or so various JSON files:

The prints are there for current debugging purposes

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

The first print gives me the JSON string, and the second print gives me null. Using json_last_error I managed to find out that PHP was refusing to parse the JSON as it was in an invalid syntax.

This is a snipped of the JSON I'm parsing ( full thing here ), it validates with every validator I can find on 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": ""
    }]
}

I have no idea why this isn't parsing as the JSON looks fine to me! Been staring at this for quite a while now so would be more than happy to here some thoughts on this.

EDIT: Copy and pasting the data from the first print_r and pushing it through the json_decode works fine. I'm assuming this means there is an issue with where the JSON is originating from which could be messing up encoding or something?

Try this:

json_decode($this->response, true);

From the documentation about the second parameter :

When TRUE, returned objects will be converted into associative arrays.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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