2015-12-07 31 views
2

http://openstates.org/api/v1/legislators/NCL000018/如何解析此JSON返回該委員會的名稱

爲立法者委員會的名字都在角色的元素。我嘗試了幾種不同的方式來做到這一點,但無法檢索所有的委員會名稱。

我想委員會:和

我的代碼的第一部分看起來像這樣

<?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循環,但就是不能把語法正確的committee_id。

+0

數組中的committe和commitee_id的鍵名是什麼? –

+0

請注意,您還需要此curl選項:'curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);' – Will

+0

感謝您的快速回復。問題解決了。所有的答案對幫助我理解替代方法都很有用。 – Vietyank

回答

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 />"; 
     } 
    } 
} 
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; 
    } 
} 
2

你可以很容易通過json_decode function.Array傳遞true作爲第二PARAM會很容易在迭代解析數組的JSON。你可以嘗試下面的代碼。

$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>"; 
      } 
     } 
    } 
} 
+0

謝謝。這與使用json返回的對象相比有什麼優勢? – Vietyank

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 
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>'; 

    } 
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>'; 
    } 

} 

看,如果有幫助。