我如何解析这个json以返回委员会的名字

时间:2015-12-07 05:05:01

标签: php json

http://openstates.org/api/v1/legislators/NCL000018/

立法者的委员会名称属于角色要素。我已经尝试了几种不同的方法,但无法检索所有的委员会名称。

我想要委员会:和委员会_id

我的代码的第一部分看起来像这样

<?php

$leg_id='NCL000018';

$request_url = "http://openstates.org/api/v1/legislators/$leg_id/";
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$legislator = json_decode($result);
print_r($legislator);

# Here is where I am stuck.

};

?>

我知道我需要一个foreach循环,但是无法正确使用语法。

6 个答案:

答案 0 :(得分:2)

试一试:

$legislator = json_decode($result);
foreach ($legislator->roles as $role) {
   if (isset($role->committee_id)) {
       echo $role->committee_id;
   }
   if (isset($role->committee)) {
       echo $role->committee;
   }
}

答案 1 :(得分:2)

您可以通过在json_decode函数中传递true作为第二个参数来轻松地解析数组中的JSON.Array在迭代中很容易。您可以尝试下面的代码。

$leg_id = 'NCL000018';

$request_url = "http://openstates.org/api/v1/legislators/$leg_id/";
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$legislator = json_decode($result, true);

if (is_array($legislator) && is_array($legislator['old_roles'])) {
    foreach ($legislator['old_roles'] as $years => $year_commity_arr) {
        foreach ($year_commity_arr as $commity_arr) {

            if (isset($commity_arr['committee'])) {
                echo $commity_arr['committee'] . "<br>";
            }
        }
    }
}

答案 2 :(得分:1)

<?php

$leg_id = 'NCL000018';
$request_url = "http://openstates.org/api/v1/legislators/$leg_id/";
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$legislator = json_decode($result);
//print_r($legislator->old_roles);

foreach ($legislator->old_roles as $items) {
    foreach ($items as $committee) {
        if (isset($committee->committee_id)) {
            echo $committee->committee_id . "<br />";
        }
    }
}

答案 3 :(得分:1)

试试这个:

$leg_id='NCL000018';
$request_url = "http://openstates.org/api/v1/legislators/$leg_id/";
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$legislator = json_decode($result);

echo '------------------------------------------';
foreach($legislator->roles as $k=>$committee){
    echo '<br/>';
    foreach($committee as $key=>$data){
        echo ($key=='committee_id') ? 'Id: '.$data.' - ':(($key=='committee')?'Name: '.$data:'');
    }
}

响应:

------------------------------------------

Id: NCC000188 - Name: Workforce and Economic Development
Id: NCC000099 - Name: Appropriations on Education / Higher Education
Id: NCC000012 - Name: Health Care
Id: NCC000100 - Name: Education / Higher Education
Id: NCC000019 - Name: State and Local Government
Id: NCC000098 - Name: Appropriations / Base Budget

答案 4 :(得分:1)

尝试以下代码

$legislator = json_decode($result);

$roles = $legislator->roles;
    foreach($roles as $role)
    {
        if(isset($role->committee_id))
            echo 'Committee Id: '. $role->committee_id;

        if(isset($role->committee))
            echo 'Commitee Role:' .$role->committee;
        echo '<br>';

    }

答案 5 :(得分:1)

试试这个:

$array = json_decode($json, true);

foreach($array['roles'] as $roles){
    if(array_key_exists('committee_id', $roles)){
        echo 'Commitee ID: ' . $roles['committee_id'] . '<br>';
    }

    if(array_key_exists('committee', $roles)){
        echo 'Commitee: ' . $roles['committee'] . '<br>';
    }

}

看,如果有帮助。