使用 PHP,解析具有深度嵌套值的 JSON 响应?

我研究了又研究,找不到答案。下面是我在调用 API 后得到的部分响应。


{

"api_version":"0.4",

"results":

    {

    "companies":[

        {

            "company":

            {

            "name":"BILL, HILLARY \u0026 CHELSEA CLINTON FOUNDATION",

            "company_number":"EXTUID_4147727",

            "jurisdiction_code":"us_dc",

            "incorporation_date":"1997-10-23",

            "dissolution_date":"2014-11-20",

            "company_type":"Non-Profit Corporation",

            "registry_url":"https://corp.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=4147727",

           "branch":"F",

            "branch_status":"branch of an out-of-jurisdiction company",

            "inactive":true,

            "current_status":"Revoked",

            "created_at":"2015-12-04T13:32:35+00:00",

            "updated_at":"2018-01-16T16:14:02+00:00",

            "retrieved_at":"2016-03-14T00:40:43+00:00",

            "opencorporates_url":"https://opencorporates.com/companies/us_dc/EXTUID_4147727",

            "previous_names":[],

            "source":

                {

                "publisher":"District of Columbia Department of Consumer and Regulatory Affairs",

                "url":"https://corp.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=4147727",

                "retrieved_at":"2016-03-14T00:40:43+00:00"},

                "registered_address":

                    {

                    "street_address":"1200 PRESIDENT CLINTON AVENUE,",

                    "locality":"LITTLE ROCK",

                    "region":"Arkansas",

                    "postal_code":"72201",

                    "country":"United States"

                    },

                }

            },

   下面是我的 PHP,我试图在其中检索、解析和显示 JSON 响应。

以下是我在尝试解析时收到的两个错误。

注意:未定义索引:公司在/opt/lampp/htdocs/OSINTTools.php 433行

警告:为第 433 行的 /opt/lampp/htdocs/OSINTTools.php 中的 foreach() 提供的参数无效

我想我收到这些错误是因为我没有正确访问这些值。有人能告诉我访问深度嵌套在这个 JSON 响应中的值的正确语法吗?提前致谢。

PS 我知道这个问题以前有人问过。我用漂亮的搜索工具找过和我类似的问题,都没有用。


侃侃尔雅
浏览 88回答 2
2回答

翻阅古今

Results 重复使用索引“company”,因此如果您试图直接使用它,将会混淆整个事情。我已经测试了以下内容,并且它有效:前提是该字符串的其余部分用缺少的括号关闭。$string = '{"api_version":"0.4","results":&nbsp; &nbsp; {&nbsp; &nbsp; "companies":[&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "company":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name":"BILL, HILLARY \u0026 CHELSEA CLINTON FOUNDATION",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "company_number":"EXTUID_4147727",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "jurisdiction_code":"us_dc",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "incorporation_date":"1997-10-23",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "dissolution_date":"2014-11-20",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "company_type":"Non-Profit Corporation",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "registry_url":"https://corp.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=4147727",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "branch":"F",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "branch_status":"branch of an out-of-jurisdiction company",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "inactive":true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "current_status":"Revoked",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "created_at":"2015-12-04T13:32:35+00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "updated_at":"2018-01-16T16:14:02+00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "retrieved_at":"2016-03-14T00:40:43+00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "opencorporates_url":"https://opencorporates.com/companies/us_dc/EXTUID_4147727",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "previous_names":[],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "source":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "publisher":"District of Columbia Department of Consumer and Regulatory Affairs",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "url":"https://corp.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=4147727",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "retrieved_at":"2016-03-14T00:40:43+00:00"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "registered_address":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "street_address":"1200 PRESIDENT CLINTON AVENUE,",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "locality":"LITTLE ROCK",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "region":"Arkansas",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "postal_code":"72201",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country":"United States"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "registered_address_in_full":"1200 PRESIDENT CLINTON AVENUE,, LITTLE ROCK, Arkansas, 72201",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "industry_codes":[],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "restricted_for_marketing":null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "native_company_number":"N00005066161"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "company":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name":"BILL, HILLARY \u0026 CHELSEA CLINTON FOUNDATION",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "company_number":"EXTUID_2698590",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "jurisdiction_code":"us_dc",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "incorporation_date":"2016-03-03",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "dissolution_date":null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "company_type":"Non-Profit Corporation",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "registry_url":"https://corponline.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=2698590",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "branch":"F",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "branch_status":"branch of an out-of-jurisdiction company",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "inactive":false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "current_status":"Active",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "created_at":"2015-12-08T12:22:52+00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "updated_at":"2020-04-19T21:38:13+00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "retrieved_at":"2020-04-19T17:55:36+00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "opencorporates_url":"https://opencorporates.com/companies/us_dc/EXTUID_2698590",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "previous_names":[],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "source":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "publisher":"District of Columbia Department of Consumer and Regulatory Affairs",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "url":"https://corponline.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=2698590",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "retrieved_at":"2020-04-19T17:55:36+00:00"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "registered_address":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "street_address":"1200 President Clinton Avenue,",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "locality":"LITTLE ROCK",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "region":"Arkansas",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "postal_code":"72201",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country":"United States"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "registered_address_in_full":"1200 President Clinton Avenue,, LITTLE ROCK, Arkansas, 72201",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "industry_codes":[],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "restricted_for_marketing":null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "native_company_number":"292233"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]}}';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //missing brackets are added here...$json =&nbsp; json_decode($string);$companies = $json->results->companies;$output = "<table>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <th>Company Name</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Incorporation Date</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Dissolution Date</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Company Type</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <th>Registry URL</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Current Status</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Address</th>&nbsp; &nbsp; </tr>";foreach($companies as $company){&nbsp; &nbsp; foreach($company as $data){&nbsp; &nbsp; $output .= "<tr>";&nbsp; &nbsp; $output .= "<td>".$data->name."<td>";&nbsp; &nbsp; $output .= "<td>".$data->incorporation_date."<td>";&nbsp; &nbsp; $output .= "<td>".$data->dissolution_date."<td>";&nbsp; &nbsp; $output .= "<td>".$data->company_type."<td>";&nbsp; &nbsp; $output .= "<td><a href='".$data->registry_url."'></a>Registry Link<td>";&nbsp; &nbsp; $output .= "<td>".$data->current_status."<td>";&nbsp; &nbsp; $output .= "<td>".$data->registered_address_in_full."<td>";}}$output .= "</tr></table>";echo $output;

隔江千里

你的第二个错误是因为你的第一个错误。您的第一个错误是因为您试图在数据中循环太深的一个级别。尝试这个:foreach($json['results']['companies'] as $company_values){&nbsp; &nbsp; $company = $company_values['company'];&nbsp; &nbsp; $output .= "<tr>";&nbsp; &nbsp; $output .= "<td>".$company['name']."<td>";&nbsp; &nbsp; $output .= "<td>".$company['incorporation_date']."<td>";&nbsp; &nbsp; $output .= "<td>".$company['dissolution_date']."<td>";&nbsp; &nbsp; $output .= "<td>".$company['company_type']."<td>";&nbsp; &nbsp; $output .= "<td><a href='".$company['registry_url']."'></a>Registry Link<td>";&nbsp; &nbsp; $output .= "<td>".$company['current_status']."<td>";&nbsp; &nbsp; $output .= "<td>".$company['registered_address_in_full']."<td>";}基本上,您的循环需要从高 1 级开始,并且在循环内您可以从键中提取值company。
打开App,查看更多内容
随时随地看视频慕课网APP