json encode is not working with array of objects

后端 未结 5 1496
故里飘歌
故里飘歌 2020-12-31 04:06

I want to convert array of objects to json encoding, I make like this

$allVisits = $mapper->getAllVisits($year, $month);
echo json_encode($allVisits);
         


        
5条回答
  •  春和景丽
    2020-12-31 04:26

    Are the properties private or protected for the object? If so, json encode can't see them inside the object. I get around this by creating a 'toJson' method in my objects that I need to serialize into json. In this method, I walk the objects properties and manually construct a generic object, that I pass to json_encode. Then I return the Json string from this method.

    Do not just make all your object properties public!!!!

提交回复
热议问题