0

The following output JSONP I'm receiving from the third part API.

processResponse({
    "website": {
        "name": "example.it/",
        "malwareListStatus": "listed",
        "partialMalwareHosts": [],
        "uwsListStatus": "unlisted",
        "partialUwsHosts": [],
        "socialListStatus": "unlisted",
        "partialSocialEngHosts": [],
        "malwareDownloadListStatus": "unlisted",
        "partialMalwareDowHosts": [],
        "uwsDownloadListStatus": "unlisted",
        "partialUwsDowHosts": [],
        "unknownDownloadListStatus": "unlisted",
        "partialUnknownDowHosts": [],
        "numAses": 1,
        "numListedTimes": 1,
        "asList": ["AS28716 (RETELIT-AS)"],
        "malwareSite": {
            "type": 6,
            "sendsToAttackSites": [],
            "sendsToIntermediarySites": [],
            "receivesTrafficFrom": ["tes.com/", "test.com/", "test.fr/"]
        }
    },
    "as": {},
    "dataUpdatedDate": 1462333750,
    "lastVisitDate": 1462321226,
    "lastMaliciousDate": 1462321226,
    "numTested": 14484
});

I'm trying to use json_decode($ApiOutput); to convert into the PHP array. However, It is not working. Do you have any pointers ?

  • 2
    Possible duplicate of [How to convert JSON string to array](http://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array) – rll May 04 '16 at 09:39

2 Answers2

1

The fastest way would be to do just a simple str_replace() to get rid of the function name and the last curly brace in the Response. Otherwise you don't get valid JSON and that's why your json_decode() fails.

Stoffo
  • 177
  • 10
0

json_decode() is the correct method.

Its working for me:

$json = '{
    "website": {
        "name": "example.it/",
        "malwareListStatus": "listed",
        "partialMalwareHosts": [],
        "uwsListStatus": "unlisted",
        "partialUwsHosts": [],
        "socialListStatus": "unlisted",
        "partialSocialEngHosts": [],
        "malwareDownloadListStatus": "unlisted",
        "partialMalwareDowHosts": [],
        "uwsDownloadListStatus": "unlisted",
        "partialUwsDowHosts": [],
        "unknownDownloadListStatus": "unlisted",
        "partialUnknownDowHosts": [],
        "numAses": 1,
        "numListedTimes": 1,
        "asList": ["AS28716 (RETELIT-AS)"],
        "malwareSite": {
            "type": 6,
            "sendsToAttackSites": [],
            "sendsToIntermediarySites": [],
            "receivesTrafficFrom": ["tes.com/", "test.com/", "test.fr/"]
        }
    },
    "as": {},
    "dataUpdatedDate": 1462333750,
    "lastVisitDate": 1462321226,
    "lastMaliciousDate": 1462321226,
    "numTested": 14484
}';
$arr = json_decode($json);
echo '<pre>';print_r($arr);echo '</pre>';

Output:

stdClass Object
(
    [website] => stdClass Object
        (
            [name] => example.it/
            [malwareListStatus] => listed
            [partialMalwareHosts] => Array
                (
                )

            [uwsListStatus] => unlisted
            [partialUwsHosts] => Array
                (
                )

            [socialListStatus] => unlisted
            [partialSocialEngHosts] => Array
                (
                )

            [malwareDownloadListStatus] => unlisted
            [partialMalwareDowHosts] => Array
                (
                )

            [uwsDownloadListStatus] => unlisted
            [partialUwsDowHosts] => Array
                (
                )

            [unknownDownloadListStatus] => unlisted
            [partialUnknownDowHosts] => Array
                (
                )

            [numAses] => 1
            [numListedTimes] => 1
            [asList] => Array
                (
                    [0] => AS28716 (RETELIT-AS)
                )

            [malwareSite] => stdClass Object
                (
                    [type] => 6
                    [sendsToAttackSites] => Array
                        (
                        )

                    [sendsToIntermediarySites] => Array
                        (
                        )

                    [receivesTrafficFrom] => Array
                        (
                            [0] => tes.com/
                            [1] => test.com/
                            [2] => test.fr/
                        )

                )

        )

    [as] => stdClass Object
        (
        )

    [dataUpdatedDate] => 1462333750
    [lastVisitDate] => 1462321226
    [lastMaliciousDate] => 1462321226
    [numTested] => 14484
)
Pupil
  • 23,834
  • 6
  • 44
  • 66