2016-04-30 90 views
1

我想從使用PHP的JSON feed中獲取一些值。出於某種原因,我嘗試了許多小時而沒有成功。從PHP中獲取JSON的值

我所試圖實現的是獲得冠軍從一個JSON提要看起來像這樣每個項目:

stdClass Object 
(
[rss] => stdClass Object 
    (
     [@attributes] => stdClass Object 
      (
       [version] => 2.0 
      ) 

     [channel] => stdClass Object 
      (
       [title] => PR.com Press Releases 
       [link] => http://www.PR.com/ 
       [description] => Latest news releases from PR.com 
       [ttl] => 60 
       [image] => stdClass Object 
        (
         [title] => PR.com Press Releases 
         [link] => http://www.PR.com/ 
         [url] => http://www.PR.com/images/logo_white.jpg 
        ) 

       [item] => Array 
        (
         [0] => stdClass Object 
          (
           [title] => Nation’s Top Online Medical Weight Loss Provider Now www.hCGTreatments.com 
           [link] => http://www.pr.com/press-release/669077 
           [guid] => http://www.pr.com/press-release/669077 
           [description] => <p><em>With an improved protocol and advancements in technology, HCGTreatments.com has become one of the most reliable names in online weight loss.</em></p> 
           [pubDate] => Sat, 30 Apr 2016 07:00:00 +0000 
           [dc_creator] => Diet Doc 
           [dc_language] => en-us 
           [dc_format] => text/html 
           [dc_identifier] => http://www.pr.com/press-release/669077 
          ) 

         [1] => stdClass Object 
          (
           [title] => Sentynl Therapeutics, Inc. Launches Its Commercial Organization 
           [link] => http://www.pr.com/press-release/669136 
           [guid] => http://www.pr.com/press-release/669136 
           [description] => <p>Sentynl Therapeutics, Inc. Launches Its Commercial Organization</p> 
           [pubDate] => Sat, 30 Apr 2016 07:00:00 +0000 
           [dc_creator] => Sentynl Therapeutics, Inc. 
           [dc_language] => en-us 
           [dc_format] => text/html 
           [dc_identifier] => http://www.pr.com/press-release/669136 
          ) 

         [2] => stdClass Object 
          (
           [title] => Professional of the Year 2015, Bruce H. Levin, is Now a Lifetime VIP in America’s Registry of Outstanding Professionals 
           [link] => http://www.pr.com/press-release/664848 
           [guid] => http://www.pr.com/press-release/664848 
           [description] => <p>Professional of the Year 2015, Bruce H. Levin, is Now a Lifetime VIP in America’s Registry of Outstanding Professionals</p> 
           [pubDate] => Sat, 30 Apr 2016 07:00:00 +0000 
           [dc_creator] => America's Registry of Outstanding Professionals 
           [dc_language] => en-us 
           [dc_format] => text/html 
           [dc_identifier] => http://www.pr.com/press-release/664848 
          ) 

        ) 

      ) 

    ) 

) 

我的PHP代碼如下所示:

$url = "http://example.com/file.json"; // just an example 
$json_file = file_get_contents("$url"); 

$json = json_decode($json_file, true); 

foreach($rss->$item as $mydata) 
{ 
echo $mydata->title . "\n"; 
} 

我曾嘗試數以百計的變化,但未能得到所有的標題..

也許有人可以指出我正確的方向。

謝謝:)

+0

'$ JSON = json_decode($ json_file,真);'這將給你一個關聯數組,和我不知道'$ rss'從哪裏來? – Vincent

+0

echo print_r($ json,true);首先看到使用foreach語句之前的結果。然後修改您的$ mydata->標題 – Gopalakrishnan

+0

可能重複[如何從JSON提取數據與PHP?](http://stackoverflow.com/questions/29308898/how-do- I-提取數據從 - JSON與 - PHP) –

回答

0

嘗試獲取對象這樣

​​