嗨,大家好我有一個問題。 ,而我是執行一些代碼,我達到了,我需要在每個對象使用的toJSON方法對象在PHP中的json
,所以我已經加入此代碼
public function toJson(){
return json_encode($this); // $this which refers to the current object
}
它已經回到剛剛{}
,所以我知道這個類裏面的點它不承認這個 類的屬性,以便相反,我試圖將其轉換像
public function toJson(){
$array=(array)$this;
return json_encode($array);
}
我得到怪異的結果
string '{"\u0000Response\u0000status":0,"\u0000Response\u0000data":null,"\u0000Response\u0000error":" type non valide "}' (length=112)
我可能最終編寫自定義的JSON對象
這樣
public function toJson(){
$myjson="{field1:data1,field2:data2}";
return $myjson;
}
,但我不希望每次我增加新屬性
我很感激,如果時間倒退回到它你有任何想法爲什麼轉換這不起作用
必須愛PHP的命名空間的想法... :-) –
你究竟試圖返回作爲JSON格式呢?該對象的屬性? – Shoe
@Jefffrey是的,但沒有與空字符錯位,我假設 –