2

我还有另一个 JSON / PHP 问题。我将首先发布一段简短的 JSON,至少足以传达我的观点:

 "results": [
    {
        "members": [
            {
                "side": "majority",
                "rank": 1,
                "title": "Chairman",
                "legislator": {
                    "bioguide_id": "T000464",
                    "birthday": "1956-08-21",
                    "chamber": "senate",
                    "contact_form": "http://www.tester.senate.gov/Contact/index.cfm",
                    "crp_id": "N00027605",
                    "district": null,
                    "facebook_id": "210573031664",
                    "fax": "202-224-8594",
                    "fec_ids": [
                        "S6MT00162"
                    ],
                    "first_name": "Jon",
                    "gender": "M",
                    "govtrack_id": "412244",
                    "icpsr_id": 40702,
                    "in_office": true,
                    "last_name": "Tester",
                    "lis_id": "S314",
                    "middle_name": null,
                    "name_suffix": null,
                    "nickname": null,
                    "office": "706 Hart Senate Office Building",
                    "party": "D",
                    "phone": "202-224-2644",
                    "senate_class": 1,
                    "state": "MT",
                    "state_name": "Montana",
                    "state_rank": "junior",
                    "term_end": "2019-01-03",
                    "term_start": "2013-01-03",
                    "thomas_id": "01829",
                    "title": "Sen",
                    "twitter_id": "testerpress",
                    "votesmart_id": 20928,
                    "website": "http://www.tester.senate.gov",
                    "youtube_id": "senatorjontester"
                }
            },
            {
                "side": "majority",
                "rank": 2,
                "title": null,
                "legislator": {
                    "bioguide_id": "P000590",
                    "birthday": "1963-01-10",
                    "chamber": "senate",
                    "contact_form": "http://www.pryor.senate.gov/public/index.cfm?p=ContactMe",
                    "crp_id": "N00013823",
                    "district": null,
                    "facebook_id": "9248638978",
                    "fax": "202-228-0908",
                    "fec_ids": [
                        "S0AR00028"
                    ],
                    "first_name": "Mark",
                    "gender": "M",
                    "govtrack_id": "300080",
                    "icpsr_id": 40301,
                    "in_office": true,
                    "last_name": "Pryor",
                    "lis_id": "S295",
                    "middle_name": null,
                    "name_suffix": null,
                    "nickname": null,
                    "office": "255 Dirksen Senate Office Building",
                    "party": "D",
                    "phone": "202-224-2353",
                    "senate_class": 2,
                    "state": "AR",
                    "state_name": "Arkansas",
                    "state_rank": "senior",
                    "term_end": "2015-01-03",
                    "term_start": "2009-01-06",
                    "thomas_id": "01701",
                    "title": "Sen",
                    "twitter_id": "senmarkpryor",
                    "votesmart_id": 35,
                    "website": "http://www.pryor.senate.gov",
                    "youtube_id": "senatorpryor"
                }
            },

好的 - 我试图获取的信息是每个立法者的头衔,以及 bioguide_id。我用来解析信息的代码如下:

 $url1 = 'http://congress.api.sunlightfoundation.com/committees?fields=members&apikey=XXXXXXXXXXX&per_page=20&page=1';
 $response1 = file_get_contents($url1);
 $key1 = json_decode($response1, true);


 foreach ($key1['results'] as $value){

$title_1 = $value['members'][0]['title'];

if($title_1 == NULL){
    $title_1 = "NULL";
}
echo $title_1 . '<br/>' . $value['members'][0]['legislator']['bioguide_id'] . '<br/>';

 }

但是,运行脚本的结果如下:

 Chairman
 T000464
 Chairman
 M001170
 Chairman
 B001265
 Chairman
 L000261
 Chairman
 B000711
 Chairman
 K000384
 Chairman
 B001267
 Chairman
 C001070
 Chairman
 W000802
 Chairman
 M001176
 Chairman
 N000032
 Chairman
 K000367
 Chairman
 G000555
 NULL
 L000174
 Chairman
 H001069
 Chairman
 B001267
 Chairman
 D000607
 Vice Chairman
 B000243
 Chairman
 S000148
 Vice Chairman
 S000148

乍一看,我认为事情看起来很可疑,因为主席头衔浮动的数量(当主席之间应该有几个 NULL 时)并且第二个 bioguide_id 不是第二个职位的正确 bioguide_id(主席和 T000464 看起来正确,但下一个不应该是主席 M001170,而是 NULL P000590)。我已将 $value['members'][0]... 切换为 $value['members'][1],并且能够获得第二个地址,但结果不正确。有没有任何人可以看到可以让我获取正确信息的东西?从我处理其他 JSON 文件的方式来看,我似乎并没有这样做非常不正确。在此先感谢(我知道它很长)。

4

1 回答 1

2

foreach正在循环遍历结果集,而不是成员集。

这是foreach我已经确认的作品:

foreach ($key1['results'][0]['members'] as $value) {
  $title_1 = $value['title'];
  if ($title_1 == NULL) {
    $title_1 = "NULL";
  }

  echo $title_1 . '<br/>' . $value['legislator']['bioguide_id'] . '<br/>';
}
于 2013-08-15T16:12:19.937 回答