简体   繁体   中英

parsing json data in php from a url request

Trying to parse the following data but having trouble getting the correct items to echo. What am I doing wrong?

{  
    "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
      },

Here is what I have for code so far:

<?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";
    };
?>

Change

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM