3

I'm getting a PHP array from a web page (as a string). It looks like :

Array
(
[k1] => Array
    (
        [a] => Array
            (
                [id] => 1
                [age] => 60
            )

        [b] => Array
            (
                [id] => 2
                [age] => 30
            )

    )

[k2] => v2
)

I want to parse it in python. Does anyone have an idea how to do this?

Thanks, Rivka

Edit: This is really not a json, like a few people commented. Thanks for the comments, and I updated the question.

7
  • what do you mean by parsing it? – Ibu May 30 '11 at 8:38
  • 1
    possible duplicate of Getting started with json – Pekka May 30 '11 at 8:39
  • 2
    That is not json - that is php – troelskn May 30 '11 at 8:39
  • This is not JSON. You'd have to do a json_encode($array); in PHP to get proper JSON. – Pekka May 30 '11 at 8:39
  • 1
    That's no JSON... If it was, it shouldn't matter where it comes from. JSON is JSON is JSON. – deceze May 30 '11 at 8:39
10

That's not JSON, that's just how PHP prints arrays. If you want to create JSON of the array, check out json_encode for PHP. Then use Python's JSON library (or here for py3) to read it.

4

If I understood you correctly, you are using print_r on array to get that output. This is a visual representation of array only, you can't really parse it. For example:

array('Array'.PHP_EOL."\t(".PHP_EOL."            [0] => test".PHP_EOL."\t)")

will look exactly like

array(array('test'));

You should use some real serializing function to do what you want(json,serialize etc.);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.