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? Commented May 30, 2011 at 8:38
  • 1
    possible duplicate of Getting started with json Commented May 30, 2011 at 8:39
  • 2
    That is not json - that is php Commented May 30, 2011 at 8:39
  • This is not JSON. You'd have to do a json_encode($array); in PHP to get proper JSON. Commented May 30, 2011 at 8:39
  • 1
    That's no JSON... If it was, it shouldn't matter where it comes from. JSON is JSON is JSON. Commented May 30, 2011 at 8:39

2 Answers 2

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.

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

Comments

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.);

Comments

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.