使用PHP-Sunlight Labs congressapi难以从JSON对象访问值

时间:2011-07-26 14:44:02

标签: php json

我正在尝试使用PHP从Sunlight Labs Congress API中提取的JSON对象中提取值。我能够将请求响应数据获取到JSON对象中,但是没有成功从对象中获取特定值。我可以使用print_r成功转储对象。

我现在已经用PHP试验了大约12个小时,所以这是一个完全无关紧要的问题......

这是PHP:     

$zip = $_REQUEST['zip'];
$sf = new SunlightLegislator;

$data = $sf->legislatorZipCode( $zip );
$leg = json_decode($data);

echo '<pre>';
print_r($data).'</pre><p>';
echo 'First Name ';

echo $leg[1]->legislator->firstname;

echo $leg[1]->firstname;
?>

PHP产生此输出:

stdClass Object
(
    [0] => stdClass Object
        (
            [legislator] => stdClass Object
                (
                    [website] => http://bass.house.gov/
                    [fax] => 202-225-2946
                    [govtrack_id] => 400019
                    [firstname] => Charles
                    [chamber] => house
                    [middlename] => 
                    [lastname] => Bass
                    [congress_office] => 2350 Rayburn House Office Building
                    [eventful_id] => 
                    [phone] => 202-225-5206
                    [webform] => 
                    [youtube_url] => 
                    [nickname] => Charlie
                    [gender] => M
                    [district] => 2
                    [title] => Rep
                    [congresspedia_url] => 
                    [in_office] => 1
                    [senate_class] => 
                    [name_suffix] => 
                    [twitter_id] => RepCharlesBass
                    [birthdate] => 1952-01-08
                    [bioguide_id] => B000220
                    [fec_id] => H0NH02017
                    [state] => NH
                    [crp_id] => N00000423
                    [official_rss] => 
                    [facebook_id] => 
                    [party] => R
                    [email] => 
                    [votesmart_id] => 22216
                )

        )

    [1] => stdClass Object
        (
            [legislator] => stdClass Object
                (
                    [website] => http://shaheen.senate.gov
                    [fax] => 
                    [govtrack_id] => 412323

                    ...SNIPPAGE...

                    [facebook_id] => SenatorShaheen
                    [party] => D
                    [email] => 
                    [votesmart_id] => 1663
                )

        )

    [2] => stdClass Object
        (
            [legislator] => stdClass Object
                (
                    [website] => http://ayotte.senate.gov 
                    [fax] => 
                    [govtrack_id] => 412493
                    [firstname] => Kelly
                    [chamber] => senate
                    [middlename] => A 

                    ...SNIPPAGE...

                    [email] => 
                    [votesmart_id] => 42352
                )

        )

)
First Name 

目前,我只是想把第一个立法者的名字输出到页面。我确定数据在那里,并且对json_decode进行错误检查不会产生任何错误。我认为我的麻烦在于我用来访问解码对象的语法。

问题: 我如何输出立法者的名字? website字段是否可能破坏对象访问?

谢谢, 肖恩

1 个答案:

答案 0 :(得分:0)

这似乎是由@soulmerge在stdClass Object problems上解决的。对于您的对象,以下内容可行:

$array = (array) $result;
$array[0]->legislator->website;