Json编码获取参数值

时间:2013-12-22 05:17:38

标签: php json

以下是我的php代码,它以json字符串形式给出结果:

<?php
$feedUrl = "http://api.frrole.com/v1/curated-content?location=India&contenttype=link&orderby=popularity&apikey=wmqmz15svzH8qSy5yQuF52b068a40a69e";
$json = file_get_contents($feedUrl);
$code = json_decode($json);
var_dump($code);
?>

从以下json结果我想得到titlecitytweets个人。我怎么能这样做?

Json结果:

  public 'remainingquota' => string '200' (length=3)
  public 'results' => 
    array (size=25)
      0 => 
        object(stdClass)[2]
          public 'timestamp' => string '2013-12-22 04:59:06' (length=19)
          public 'id' => string '414360237391376384' (length=18)
          public 'category' => string '' (length=0)
          public 'username' => string 'EarthPix' (length=8)
          public 'displayname' => string 'EarthPix' (length=8)
          public 'profileimage' => string 'http://pbs.twimg.com/profile_images/378800000408926177/3c54c23ddce812cef8c1a0e2141e4505_bigger.jpeg' (length=99)
          public 'tweet_text' => string 'Hotel in Mumbai. Each room has its own swimming pool. http://t.co/OtqIQGc7aR' (length=76)
          public 'title' => string 'Hotel in Mumbai. Each room has its own swimming pool. http://t.co/OtqIQGc7aR' (length=76)
          public 'city' => string 'mumbai' (length=6)
          public 'rt_count' => string '363' (length=3)
          public 'sentiment' => string '0' (length=1)
          public 'powertweet' => string '0' (length=1)
          public 'entity' => 
            array (size=1)
              ...
          public 'updated' => string '17:32:46' (length=8)
      1 => 
        object(stdClass)[4]
          public 'timestamp' => string '2013-12-22 04:38:28' (length=19)
          public 'id' => string '414284358606790657' (length=18)
          public 'category' => string '' (length=0)
          public 'username' => string 'KapilSharmaK9' (length=13)
          public 'displayname' => string 'KapilSharmaK9' (length=13)
          public 'profileimage' => string 'http://pbs.twimg.com/profile_images/378800000771389653/d68e47b2738dd1c83277dcac2ad1a6da_normal.jpeg' (length=99)
          public 'tweet_text' => string 'Receiving INDIAN OF THE YEAR award from respected Amol palekar.. Thnk u god n my fans. http://t.co/L4y8OVc4we' (length=109)
          public 'title' => string 'Receiving INDIAN OF THE YEAR award from respected Amol palekar.. Thnk u god n my fans. http://t.co/L4y8OVc4we' (length=109)
          public 'city' => string 'mumbai' (length=6)
          public 'rt_count' => string '177' (length=3)
          public 'sentiment' => string '40' (length=2)
          public 'powertweet' => string '0' (length=1)
          public 'entity' => 
            array (size=1)
              ...
          public 'updated' => string '22:34:17' (length=8)
      2 => 
        object(stdClass)[7]
          public 'timestamp' => string '2013-12-22 04:55:20' (length=19)
          public 'id' => string '414603019536584704' (length=18)
          public 'category' => string 'celeb' (length=5)
          public 'username' => string 'arrahman' (length=8)
          public 'displayname' => string 'A.R.Rahman' (length=10)
          public 'profileimage' => string 'http://a0.twimg.com/profile_images/2967359603/450c0df90b3eb6711318c450db7db201_bigger.jpeg' (length=90)
          public 'tweet_text' => string 'Thank you listeners for Maryan being chosen as the 'Tamil Album of Year' in iTunes' Best of 2013 http://t.co/hiZC3HI5fk' (length=119)
          public 'title' => string 'Thank you listeners for Maryan being chosen as the 'Tamil Album of Year' in iTunes' Best of 2013 http://t.co/hiZC3HI5fk' (length=119)
          public 'city' => string 'chennai' (length=7)
          public 'rt_count' => string '117' (length=3)
          public 'sentiment' => string '0' (length=1)
          public 'powertweet' => string '1' (length=1)
          public 'entity' => 
            array (size=1)
              ...
          public 'updated' => string '01:28:02' (length=8)
      3 => 

UPDATE1

我已经在网上尝试过几个例子,但却收到错误。相反,downvoting实现了解决编程问题的目的。 SO的代码

我的尝试:var_dump($array["username"]);

UPDATE2 的print_r($代码);

给出:

stdClass Object ( [status] => ok [message] => Success v1. [remainingquota] => 200 [results] => Array ( [0] => stdClass Object ( [timestamp] => 2013-12-22 05:09:45 [id] => 414360237391376384 [category] => [username] => EarthPix [displayname] => EarthPix [profileimage] => http://pbs.twimg.com/profile_images/378800000408926177/3c54c23ddce812cef8c1a0e2141e4505_bigger.jpeg [tweet_text] => Hotel in Mumbai. Each room has its own swimming pool. http://t.co/OtqIQGc7aR [title] => Hotel in Mumbai. Each room has its own swimming pool. http://t.co/OtqIQGc7aR [city] => mumbai [rt_count] => 369 [sentiment] => 0 [powertweet] => 0 [entity] => Array ( [0] => stdClass Object ( [url] => http://pbs.twimg.com/media/BZJq8raIEAAPPmZ.jpg [type] => img ) ) [updated] => 17:43:47 ) [1] => stdClass Object ( [timestamp] => 2013-12-22 05:03:12 [id] => 414284358606790657 [category] => [username] => KapilSharmaK9 [displayname] => KapilSharmaK9 [profileimage] => http://pbs.twimg.com/profile_images/378800000771389653/d68e47b2738dd1c83277dcac2ad1a6da_normal.jpeg [tweet_text] => Receiving INDIAN OF THE YEAR award from respected Amol palekar.. 

2 个答案:

答案 0 :(得分:1)

试试这个......

  <?php
    error_reporting(E_ERROR | E_PARSE);
    $feedUrl = "http://api.frrole.com/v1/curated-content?location=India&contenttype=link&orderby=popularity&apikey=wmqmz15svzH8qSy5yQuF52b068a40a69e";
    $json = file_get_contents($feedUrl);
    $code = json_decode($json,true);

    foreach($code as $arr)
    {
    foreach($arr as $k=>$v)
    {
    echo $arr[$k]['title']."<br>";
    echo $arr[$k]['city']."<br>";
    echo "<br>";
    }
    }

输出

Hotel in Mumbai. Each room has its own swimming pool. http://t.co/OtqIQGc7aR
mumbai

Receiving INDIAN OF THE YEAR award from respected Amol palekar.. Thnk u god n my fans. http://t.co/L4y8OVc4we
mumbai

Thank you listeners for Maryan being chosen as the 'Tamil Album of Year' in iTunes' Best of 2013 http://t.co/hiZC3HI5fk
chennai

I just voted for The Times of India Entertainment - Best of 2013 for the Movie of the Year- #CE!! http://t.co/kT9csPQaR8
india

答案 1 :(得分:0)

之后

$code = json_decode($json);

你得到一个对象。 $code->results是一个对象数组。

所以你的代码将是

foreach ($code->results as $result) {
    echo $result->title;
    echo "<br/>";
    echo $result->city;
    echo "<br/>";
}

此外,如果您需要数组而不是对象,则需要更改如下:

$code = json_decode($json, true);

foreach ($code['results'] as $result) {
    echo $result['title'];
    echo "<br/>";
    echo $result['city'];
    echo "<br/>";
}

查看json_decode()如何构建Json optput。