2016-01-13 119 views
0

我試圖解碼JSON陣列,但是我得到以下錯誤:PHP:問題解碼JSON陣列

Object of class stdClass could not be converted to string

的JSON編碼數組如下所示:

echo json_decode('{ 
    "type": "FeatureCollection", 
    "features": [ 
     { 
     "type": "Feature", 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
     149.23531999999997, 
     -35.352484 
     ] 
     }, 
     "properties": { 
     "Fcilty_typ": "MO", 
     "gx_id": "1" 
     } 
     } 
    ] 
    }' 
); 

我爲了保持這個查詢的更緊湊,我已經將一些字段從這裏剝離出來。任何人都可以提出問題在這裏?

感謝

+2

答案已經存在問題.. json_decode將返回對象沒有字符串 –

回答

2

在JSON,

{字符表示對象的開始。

[字符表示數組的開始。

所以,當你做一個json_decode()它需要一個字符串,並且無論json結構表示什麼。因此,在你的情況下,它會創建stdClass類型與性質等

您不能echo對象的PHP對象,但你可以在兩個陣列,可以使用這些功能和對象

print_r() a nice simple display of the data 

var_dump() a more complex to read display of the data in my opinion 
      but it does show data types and sizes 

var_export() similiar to print_r() 

您還可以添加第二個paameter到json_decode($string, true),它會將在數據結構的json字符串表示中找到的對象轉換爲數組,儘管我從來沒有見過這種需要。

2

你可以不回你解碼JSON(這是一個對象),但是,您可以嘗試var_dump

var_dump(json_decode('{ 
    "type": "FeatureCollection", 
    "features": [ 
     { 
     "type": "Feature", 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
     149.23531999999997, 
     -35.352484 
     ] 
     }, 
     "properties": { 
     "Fcilty_typ": "MO", 
     "gx_id": "1" 
     } 
     } 
    ] 
    }' 
)); 
0

您試圖echo類和echo只是打印字符串。

嘗試將echo更改爲var_dump

var_dump會告訴你類的屬性。

0

您正在解碼JSON數據並將其回顯。不要忘記,回聲只適用於字符串,JSON解碼返回另一個對象。