为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
侃侃尔雅 2023-04-15 11:03:33
我研究了又研究,找不到答案。下面是我在调用 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 我知道这个问题以前有人问过。我用漂亮的搜索工具找过和我类似的问题,都没有用。
查看完整描述

2 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

Results 重复使用索引“company”,因此如果您试图直接使用它,将会混淆整个事情。我已经测试了以下内容,并且它有效:前提是该字符串的其余部分用缺少的括号关闭。


$string = '{

"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"

                    },

                "registered_address_in_full":"1200 PRESIDENT CLINTON AVENUE,, LITTLE ROCK, Arkansas, 72201",

                "industry_codes":[],

                "restricted_for_marketing":null,

                "native_company_number":"N00005066161"

                }

            },

        {

            "company":

            {

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

            "company_number":"EXTUID_2698590",

            "jurisdiction_code":"us_dc",

            "incorporation_date":"2016-03-03",

            "dissolution_date":null,

            "company_type":"Non-Profit Corporation",

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

            "branch":"F",

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

            "inactive":false,

            "current_status":"Active",

            "created_at":"2015-12-08T12:22:52+00:00",

            "updated_at":"2020-04-19T21:38:13+00:00",

            "retrieved_at":"2020-04-19T17:55:36+00:00",

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

            "previous_names":[],

            "source":

                {

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

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

                "retrieved_at":"2020-04-19T17:55:36+00:00"},

                "registered_address":

                    {

                    "street_address":"1200 President Clinton Avenue,",

                    "locality":"LITTLE ROCK",

                    "region":"Arkansas",

                    "postal_code":"72201",

                    "country":"United States"

                    },

                "registered_address_in_full":"1200 President Clinton Avenue,, LITTLE ROCK, Arkansas, 72201",

                "industry_codes":[],

                "restricted_for_marketing":null,

                "native_company_number":"292233"

                }

            }]}}';                              //missing brackets are added here

...


$json =  json_decode($string);

$companies = $json->results->companies;


$output = "

<table>

    <tr>

        <th>Company Name</th>

        <th>Incorporation Date</th>

        <th>Dissolution Date</th>

        <th>Company Type</th>                   

        <th>Registry URL</th>

        <th>Current Status</th>

        <th>Address</th>

    </tr>


";



foreach($companies as $company){

    foreach($company as $data){


    $output .= "<tr>";

    $output .= "<td>".$data->name."<td>";

    $output .= "<td>".$data->incorporation_date."<td>";

    $output .= "<td>".$data->dissolution_date."<td>";

    $output .= "<td>".$data->company_type."<td>";

    $output .= "<td><a href='".$data->registry_url."'></a>Registry Link<td>";

    $output .= "<td>".$data->current_status."<td>";

    $output .= "<td>".$data->registered_address_in_full."<td>";


}

}

$output .= "</tr></table>";

echo $output;


查看完整回答
反对 回复 2023-04-15
?
隔江千里

TA贡献1906条经验 获得超10个赞

你的第二个错误是因为你的第一个错误。您的第一个错误是因为您试图在数据中循环太深的一个级别。


尝试这个:


foreach($json['results']['companies'] as $company_values){


    $company = $company_values['company'];


    $output .= "<tr>";

    $output .= "<td>".$company['name']."<td>";

    $output .= "<td>".$company['incorporation_date']."<td>";

    $output .= "<td>".$company['dissolution_date']."<td>";

    $output .= "<td>".$company['company_type']."<td>";

    $output .= "<td><a href='".$company['registry_url']."'></a>Registry Link<td>";

    $output .= "<td>".$company['current_status']."<td>";

    $output .= "<td>".$company['registered_address_in_full']."<td>";

}

基本上,您的循环需要从高 1 级开始,并且在循环内您可以从键中提取值company。


查看完整回答
反对 回复 2023-04-15
  • 2 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信