从url请求解析php中的json数据

时间:2018-02-01 13:29:10

标签: php arrays json parsing

尝试解析以下数据,但无法获得正确的项目以进行回应。我做错了什么?

{  
    "total_results":12585,
    "organizations":[  
      {  
         "ein":232669352,
         "strein":"23-2669352",
         "name":"AMERICAN HOME LIFE INTERNATIONAL INC",
         "sub_name":"AMERICAN HOME LIFE INTERNATIONAL INC",
         "city":"LANCASTER",
         "state":"PA",
         "ntee_code":"Q220",
         "raw_ntee_code":"Q220",
         "subseccd":3,
         "has_subseccd":true,
         "have_filings":true,
         "have_extracts":true,
         "have_pdfs":true,
         "score":417.01215
      },
      {  
         "ein":251691788,
         "strein":"25-1691788",
         "name":"HOME LIFE MINISTRIES",
         "sub_name":"HOME LIFE MINISTRIES",
         "city":"ANNVILLE",
         "state":"PA",
         "ntee_code":"X20",
         "raw_ntee_code":"X20",
         "subseccd":3,
         "has_subseccd":true,
         "have_filings":true,
         "have_extracts":true,
         "have_pdfs":true,
         "score":147.28578
      },
      {  
         "ein":240782824,
         "strein":"24-0782824",
         "name":"MIFFLINBURG AMERICAN LEGION HOME ASSOCIATION INC",
         "sub_name":"MIFFLINBURG AMERICAN LEGION HOME ASSOCIATION INC",
         "city":"MIFFLINBURG",
         "state":"PA",
         "ntee_code":null,
         "raw_ntee_code":null,
         "subseccd":7,
         "has_subseccd":true,
         "have_filings":true,
         "have_extracts":true,
         "have_pdfs":true,
         "score":145.12933
      },

到目前为止,我的代码是:

<?php
    $url = "https://projects.propublica.org/nonprofits/api/v2/search.json?
    q=%22american%20home%20life%20international%20inc%22&state%5Bid%5D=PA";
    $json = file_get_contents($url);
    $json_data = json_decode($json, true);
    foreach ($json['organizations'] as $address)
    {
        echo "items:". $address['ein'] ."\n";
    };
?>

1 个答案:

答案 0 :(得分:1)

更改

foreach ($json['organizations'] as $address)

foreach ($json_data['organizations'] as $address)