2

I need to grab a json-string from this page: https://retracted.com

If you view the source, I json-string starts after var mycarousel_itemList =. I need to parse this string as a correct json-array in my php-script.

How can this be done?

EDIT: I've managed to pull this off using explode, but the method is ugly as heck. Is there no build-in function to translate this json-string to a array?

To clarify: I want the string I grab (which is correct json) to be converted into a php-array.

6
  • 2
    Do you have permission from the copyright owner? Commented Nov 18, 2011 at 11:09
  • 1
    Yes I do. I work for a company making a collaborate homepage for this bank and this estate-selling-company. We were trying to fetch this information using their api, but they use so long to respond, so I figure I could just rip it from their homepage. And again, to clarify; I do have permission. Commented Nov 18, 2011 at 11:14
  • And your username is OptimusCrime . . . :) Commented Nov 18, 2011 at 11:28
  • @Adam Hopkinson, You don't need permission to parse a site. Commented Jan 14, 2022 at 13:22
  • @Optimus1 apart from when you do Commented Jan 14, 2022 at 16:54

2 Answers 2

1

The JSON in the script block is invalid and needs to be massaged a bit before it can be used in PHP's native json_decode function. Assuming you have already extracted the JSON string from the markup (make sure you exclude the semicolon at the end):

$json = <<< JSON
[ { address: 'Arnegårdsveien 32', … } ]
JSON;

var_dump(
    json_decode(
        str_replace(
            array(
                'address:',
                'thumb:',
                'description:',
                'price:',
                'id:',
                'size:',
                'url:',
                '\''
            ),
            array(
                '"address":',
                '"thumb":',
                '"description":',
                '"price":',
                '"id":',
                '"size":',
                '"url":',
                '"'
            ),
            $json
        )
    ,
    true
    )
);

This will then give an array of arrays of the JSON data (demo).

In other words, the properties have to be double quoted and the values need to be in double quotes as well. If you want an array of stdClass objects instead for the "{}" parts, remove the true.

You can do this either with str_replace as shown above or with a regular expression:

preg_match('
    (.+var mycarousel_itemList = ([\[].+);.+function?)smU',
    file_get_contents('http://bolig…'),
    $match
);
$json = preg_replace(
    array('( ([a-z]+)\:)sm', '((\'))'),
    array('"$1":', '"'),
    $match[1]
);
var_dump(json_decode($json, true));

The above code will fetch the URL, extract the JSON, fix it and convert to PHP (demo).

Sign up to request clarification or add additional context in comments.

Comments

1

Once you have your json data, you can use json_decode (PHP >= 5.2) to convert it into a PHP object or array

3 Comments

In the updated question, it says he's managed to pull this off (out?) using explode, "is there no build-in function to translate this json-string to a array?"
json_decode cannot decode the string though. string is broken.
@adam : I managed to do what I wanted, but I had to use 5 explodes and a few loops. I was just wondering if I could avoid this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.