2012-09-24 19 views
0

我有一個JSON飼料是私人(通過IP地址),我可以使用命令解析JSON提要到PHP,然後處理數據?

$json = file_get_contents("http://#"); 

這是進是什麼樣子,並命令作品解析 - 它正確轉儲數據使用json_decode時($ JSON);或者json_decode($ json,true);我做了一個var轉儲。

{ 
    "Holder": [ 
     { 
      "name": "Subholder One", 
      "operators": [ 
     { 
      "username": "User1", 
      "status": 3 
     }, 
     { 
      "username": "User2", 
      "status": 3 
     }, 
     { 
      "username": "User3", 
      "status": 3 
     }, 
     { 
      "username": "User4", 
      "status": 1 
     } 
     ] 
    }, 
    { 
     "name": "Subholder Two", 
     "operators": [ 
     { 
      "username": "User5", 
      "status": 3 
     } 
     ] 
    }, 
    { 
     "name": "Subholder Three", 
     "operators": [ 
     { 
      "username": "User6", 
      "status": 3 
     } 
     ] 
    }, 
    { 
     "name": "Subholder Four", 
     "operators": [ 
     { 
      "username": "User7", 
      "status": 1 
     }, 
     { 
      "username": "User8", 
      "status": 3 
     }, 
     { 
      "username": "User9", 
      "status": 1 
     } 
     ] 
    } 
    ] 
} 

當我使用簡單,下面有一行數據的代碼,用沒有的file_get_contents但數據線工作原理:

$obj = json_decode($json); 
echo $obj->username; 
echo $obj->status; 

當我使用的file_get_contents它不工作。

我希望能夠做的就是顯示所有數據的用戶名和狀態(將數字轉換成我已經完成的一個詞組),並且通過用戶名單獨顯示。

例如在用戶1的個人資料頁面上,我想解析他們的「用戶名」和「狀態」,將狀態號碼轉換爲相應的短語並顯示在屏幕上。

我在這裏撓我的頭...所以任何幫助感激地收到。

+0

你還沒有得到有效的JSON數據。 –

+1

將file_get_contents結果粘貼到此處。也許這個功能不允許通過http獲取內容? (php.ini) – mblaettermann

+0

你可以將json_decode之前的結果粘貼到file_get_contents中嗎?因爲你在上面的問題粘貼的JSON代碼是有效的 – GBD

回答

0

嘗試

$obj = json_decode($json, true); 

    echo $obj['username']; 
    echo $obj['status']; 
+0

請檢查問題。他已經試過你的解決方案 – GBD

+0

謝謝你已經嘗試過 - 不起作用:o(當我手動編寫它的數據的代碼,從飼料中提取它不。 – webservicesco

+0

大家好,有人幫忙嗎?仍然沒有工作:( – webservicesco

0

試試這個:

$request_url ="http://mysamplefeed.com/"; 
$requests = file_get_contents($request_url); 
$my_response = json_decode($requests); 
foreach($my_response->Holder as $item){ 
'<p>' .$item->username; . '</p>' 
'<p>' .$item->status; . '</p>' 
}