0

I have a JSON string that I have just converted into a stdObject using this line of code:

$stdResponse = json_decode($jsonResponse);

This give me this object:

[02-Jul-2019 16:47:00 UTC] stdClass Object
(
    [uuid] => qj/VA9z2SZKF6bT5FboOWf#$
    [id] => 653070384
    [topic] => Mark Test Meeting
)

Now I want to access the members of this object, for example the UUID. I tried just doing $stdResponse->uuid but that's an error.

THEN I tried converting the stdObject into the object that I really want using this PHP class:

class zoom_meeting
{
  public $uuid;
  public $id;
  public $topic;

  public function getUUID()
  {
    return ($this->uuid);
  }

  public function getMeetingId()
  {
    return ($this->id);
  }  

  public function getMeetingName()
  {
    return ($this->topic);
  }    
}

I did that by using this line of code and a "cast" function I found elsewhere on this forum (that seems to work according to the comments):

  $castMeeting = cast($stdResponse, "zoom_meeting");

Where the function cast is:

function cast($instance, $className)
{
    return unserialize(sprintf(
        'O:%d:"%s"%s',
        strlen($className),
        $className,
        strstr(strstr(serialize($instance), '"'), ':')
    ));
}

It looked like it worked. Here is the object now:

[02-Jul-2019 16:47:00 UTC] CASTED MEETING:
[02-Jul-2019 16:47:00 UTC] zoom_meeting Object
(
    [uuid] => qj/VA9z2SZKF6bT5FboOWf#$
    [id] => 653070384
    [topic] => Mark Test Meeting
)

Then I tried to use the get methods to "get" the information that I need out of this class object and here is the output of each call:

error_log(print_r($castMeeting->getUUID(), true));
[02-Jul-2019 16:47:00 UTC]  1
error_log(print_r($castMeeting->getMeetingId(), true));
[02-Jul-2019 16:47:00 UTC]  1
error_log(print_r($castMeeting->getMeetingName(), true));
[02-Jul-2019 16:47:00 UTC]  1

Just "1"'s and that's it. Clearly I am not getting the data I was expecting. Can anyone tell me what is going wrong here? Is there a better/cleaner way of just getting to the uuid, id, and topic values?

Any help or ideas would be MOST appreciated - Mark

  • See: [Object Serialization](https://www.php.net/manual/en/language.oop5.serialization.php), [Preferred method to store PHP arrays (json_encode vs serialize)](https://stackoverflow.com/questions/804045/preferred-method-to-store-php-arrays-json-encode-vs-serialize) – ficuscr Jul 02 '19 at 17:35
  • What error are you getting when you try to access `$stdResponse->uuid`? – DaCurse Jul 02 '19 at 17:36
  • 2
    What is your original JSON string? – Nigel Ren Jul 02 '19 at 17:38

2 Answers2

0

I believe you have a key named 02-Jul-2019 16:47:00 UTC and a subarray inside that.
I assume the key name is dynamic, so hardcoding it is not an option.
Therefore foreach the object and echo the subarray items.

foreach($stdResponse as $response){
    echo $response['uuid'];
    //echo $response['id'];
    //echo $response['topic'];
}
Andreas
  • 23,610
  • 6
  • 30
  • 62
0

I'd like to provide an alternative solution.

The transformation from your json can be an explicit method on your class. I call it a "named constructor".

class zoom_meeting
{
  private $uuid;
  private $id;
  private $topic;

  public function __construct($uuid, $meetingid, string $meetingname)
  {
      $this->uuid = $uuid;
      $this->meetingid = $meetingid;
      $this->topic = $meetingname;
  }

  public static function fromJSON(string $json): self
  {
      $data = json_decode($json);

      return new self($data->uuid, $data->meetingid, $data->meetingname);
  }

  public function getUUID()
  {
    return ($this->uuid);
  }

  public function getMeetingId()
  {
    return ($this->id);
  }  

  public function getMeetingName()
  {
    return ($this->topic);
  }    
}

No magic involved (which is a good thing)

Patrick
  • 1,562
  • 1
  • 16
  • 33