Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question Difference between JSON and stdClass

    HI

    I am wondering what is the different between a JSON object and PHP's stdClass object? What can a JSON object do that stdClass object cannot?

    Please note here I am talking only about the objects.


    Thanks

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    there is no JSON object in PHP.

    if you decode a JSON string you either get an associative array or a stdClass object (if applicable), depending on the used flag.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Dormilich View Post
    there is no JSON object in PHP.

    you either get an associative array or a stdClass object (if applicable), depending on the used flag.

    So when decoding a json string if we get an stdclass object, what difference will it be from a generic php stdClass object?

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    depends on how you define "generic php stdClass object" probably the biggest difference is that it has properties.

    Converting to object

    If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. Arrays convert to an object with properties named by keys, and corresponding values. For any other value, a member variable named scalar will contain the value.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Dormilich View Post
    depends on how you define "generic php stdClass object"
    This way...

    $obj = new stdClass();

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    then the only difference is that json_decode() puts properties in the instantiated class
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    New to the CF scene
    Join Date
    Aug 2014
    Location
    Indaia, Dlhi
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I am trying to decode a JSON string into an array but i get the following error.

    Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\temp\asklaila.php on line 6

    Here is the code:

    <?php
    $json_string = 'http://www.domain.com/jsondata.json';

    $jsondata = file_get_contents($json_string);
    $obj = json_decode($jsondata);
    print_r($obj['Result']);
    ?>

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by Elly1 View Post
    I am trying to decode a JSON string into an array but i get the following error.

    Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\temp\asklaila.php on line 6
    well, that happens when you try array access on an object that doesn’t implement the ArrayAccess interface.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •