I can not use json_encode when convert from object

code

<?php
    require "connect.php";
    $query = "SELECT DISTINCT * FROM playlist ORDER BY rand() LIMIT 3";
    class PlaylistToday{
        public $id; // Tên thuộc tính phải trùng với tên cột 'id' trong cơ sở dữ liệu
        public $title; // Tên thuộc tính phải trùng với tên cột 'title' trong cơ sở dữ liệu
        public $artUrl; // Tên thuộc tính phải trùng với tên cột 'artUrl' trong cơ sở dữ liệu

        function __construct($id, $title, $artUrl) {
            $this->id = $id;
            $this->title = $title;
            $this->artUrl = $artUrl;
        }
    }
    $arrayplaylistfortoday = array();
    
    $data = mysqli_query($conn, $query);
    
    while($row = mysqli_fetch_assoc($data)) {
      
        array_push($arrayplaylistfortoday, new PlaylistToday($row['id']
                                                            ,$row['title']
                                                            ,$row['artUrl']));
                                                        
                                                                                                
    }
    // var_dump($arrayplaylistfortoday);
    // echo json_encode($arrayplaylistfortoday);



?>

var_dump($arrayplaylistfortoday); This code works but that code does not ( echo json_encode($arrayplaylistfortoday):wink:

Hi hoangahai23,

Self-declared class PlaylistToday does not natively support casting to arrays and hence cannot be json_encoded. If you ultimately need json, you need not use a class to re-encapsulate the data, just use a normal array.

Please format your code when posting a support request, thank you.

Cheers!

7 Likes

Have you tried var_dumping json_encode($arrayplaylistfortoday) and seeing what it gives you?

I’m with @chiucs123: for json_encode to work, the data must actually be serializable. var_dump will display whatever you put into it, but json_encode is more strict.

That’s one way to do it, yes.

Another way to do this is to have your PlaylistToday implement the JsonSerializable interface. That way, you can tell how an object of that class should be converted to JSON.

4 Likes

i tried implement but that is not work

    <?php
        require "connect.php";
        $query = "SELECT DISTINCT * FROM playlist ORDER BY rand() LIMIT 3";
        class PlaylistToday implements JsonSerializable {
    public $id;
    public $title;
    public $artUrl;

public function jsonSerialize() {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'artUrl' => $this->artUrl,
        ];
    }
    public function __construct($id, $title, $artUrl) {
        $this->id = $id;
        $this->title = $title;
        $this->artUrl = $artUrl;
    }

    
}
        
        $arrayplaylistfortoday = array();
        
        $data = mysqli_query($conn, $query);
        
        while($row = mysqli_fetch_assoc($data)) {
        
            array_push($arrayplaylistfortoday, new PlaylistToday($row['id']
                                                                ,$row['title']
                                                                ,$row['artUrl']));
                                                            
                                                                                                    
        }
        // var_dump($arrayplaylistfortoday);
        echo json_encode($arrayplaylistfortoday);

        

    ?>

when I use 00webhost, I don’t make that mistake, this means my conversion to json was not wrong

Have you tried doing this in this hosting?

3 Likes

I’m sorry, I was wrong. I thought you couldn’t just convert an object to JSON, but it appears that you can:

If a value to be serialized is an object, then by default only publicly visible properties will be included. Alternatively, a class may implement JsonSerializable to control how its values are serialized to JSON.

From: PHP: json_encode - Manual

So the code should just work.

So now my question is the same as @ChrisPAR’s: what does the json_encode function actually return? Can you make that visible with var_dump?

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.