2016-01-22 17 views
0

此代碼是工作前2天,但現在我得到一個錯誤:注:試圖獲得非對象的屬性在C: XAMPP htdocs中 ihelploginapi 上線的index.php 5

Trying to get property of non-object in C:\xampp\htdocs\ihelploginapi\index.php on line 4.

有人請幫助我。

<?php 

    $json = file_get_contents('php://input'); 
    $obj = json_decode($json,TRUE); 
    $tag = $obj->{'tag'}; 
?> 
+0

你可以'var_dump($ obj)'在json_decode之後的行併發布結果嗎? – Scott

+0

錯誤:解析錯誤:語法錯誤,意想不到 '$標籤'(T_VARIABLE)在C:\ XAMPP \ htdocs中\ ihelploginapi \的index.php上線.. –

+0

代碼:<?PHP \t $ JSON =的file_get_contents('PHP: //輸入'); \t $ obj = json_decode($ json,TRUE); \t var_dump($ obj) \t $ tag = $ obj - > {'tag'}; \t if($ tag =''){ //獲得標籤 $ tag = $ obj - > {'tag'}; –

回答

1

json_decode不給你一個對象。它給你一個數組。 你要訪問它,例如:

$tag = $obj['tag'];

或更準確地重新寫了var名

$json = file_get_contents('php://input'); 
$php_array = json_decode($json,TRUE); 
$tag = $php_array['tag']; 
0

在相關線路使用:

$tag = $obj['tag']; 
0

第二json_decode()的參數告訴它將JSON對象轉換爲PHP關聯數組,而不是PHP對象。所以你需要使用$obj['tag']而不是$obj->tag。或者將解碼線更改爲

$obj = json_decode($json); 
相關問題