#1
  1. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4

    Question Problem with output of json_decode


    I am trying to pass input to a PHP program via AJAX. The resulting array is not what I expected. A sub array gets mangled as can be seen in the test command line program below.


    The test PHP program looks like this:

    Code:
    #!/usr/bin/php
    <?php
    $newgame = '{"gname": "game name","boxid": "7","players": [{"0": "pname0"},{"1": "pname1"},{"2": "pname2"}]}';
    $game = json_decode($newgame, true);
    $textgame = json_encode($game);
    $errorgame = json_last_error();
    var_dump($newgame);
    var_dump($textgame);
    var_dump($errorgame);
    ?>
    And the output looks like this:


    Code:
    #!/usr/bin/php
    string(96) "{"gname": "game name","boxid": "7","players": [{"0": "pname0"},{"1": "pname1"},{"2": "pname2"}]}"
    string(86) "{"gname":"game name","boxid":"7","players":[["pname0"],{"1":"pname1"},{"2":"pname2"}]}"
    int(0)
    As you can see, decoding and encoding returns a different and erronious string.
    I would appreciate any advice on the format of the newgame string or any other comments on this problem.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,961
    Rep Power
    9397
    It's not erroneous: json_decode() with $assoc=true is potentially lossy. Arguably not though because {"0":"pname0"} and ["pname0"] will behave nearly identically in Javascript.

    Pass $assoc=false.
    PHP Code:
    $game json_decode($newgame); 

    Comments on this post

    • richpri agrees : Good point.
  4. #3
  5. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4

    Smile


    I found a string that works.
    Here is the new test output from same program with new string:
    Code:
    string(77) "{"gname": "game name","boxid": "7","players": ["pname0", "pname1", "pname2"]}"
    string(72) "{"gname":"game name","boxid":"7","players":["pname0","pname1","pname2"]}"
    int(0)
    The strings are the same except for whitespace.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    I'm not sure what the problem even is here. The objects are still the same, regardless of what the keys end up being. Since JS doesn't support string keys, the whole exercise is kind of moot.

    Encoding and decoding the string you gave us produces the same array:



    PHP Code:
    $newgame '{"gname": "game name","boxid": "7","players": [{"0": "pname0"},{"1": "pname1"},{"2": "pname2"}]}';
    $game json_decode($newgametrue);
    $textgame json_encode($game);
    $errorgame json_last_error();
    print_r($game);
    print_r(json_decode($textgame,true)); 
    Produces:
    Code:
    Array
    (
        [gname] => game name
        [boxid] => 7
        [players] => Array
            (
                [0] => Array
                    (
                        [0] => pname0
                    )
    
                [1] => Array
                    (
                        [1] => pname1
                    )
    
                [2] => Array
                    (
                        [2] => pname2
                    )
    
            )
    
    )
    
    Array
    (
        [gname] => game name
        [boxid] => 7
        [players] => Array
            (
                [0] => Array
                    (
                        [0] => pname0
                    )
    
                [1] => Array
                    (
                        [1] => pname1
                    )
    
                [2] => Array
                    (
                        [2] => pname2
                    )
    
            )
    
    )

    Comments on this post

    • richpri agrees : The second version works for me. My problem has been solved.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo