arrays - object to json in php -
hi guys got problem . while implementing code reached point need use tojson method in each object
so inside class have added code
public function tojson(){ return json_encode($this); // $this refers current object } it has returned {} knew not recognize properties of class instead have tried convert
public function tojson(){ $array=(array)$this; return json_encode($array); } i got weird result
string '{"\u0000response\u0000status":0,"\u0000response\u0000data":null,"\u0000response\u0000error":" type non valide "}' (length=112) i write customized json object
like
public function tojson(){ $myjson="{field1:data1,field2:data2}"; return $myjson; } but not want return each time add new property
i appreciate if have idea why converting not work
you need convert object properties array before encoding json:
public function tojson(){ return json_encode(get_object_vars($this)); } as can see, can use get_object_vars accomplish that.
Comments
Post a Comment