用 PHP 解析复杂的 JSON 响应

缺口

如果这是一个愚蠢的问题,我提前道歉!我已经阅读了解码 JSON 响应,但我似乎无法将此响应转换为可用的格式。

我正在通过 Wordpress 插件提交 GET 请求。为了预览输出以便我知道如何解析它,我使用了代码

print_r($response, true)

我得到的结果是:

Array
(
[headers] => Requests_Utility_CaseInsensitiveDictionary Object
    (
        [data:protected] => Array
            (
                [date] => Fri, 09 Jun 2017 21:48:03 GMT
                [server] => 
                [cache-control] => no-store, no-cache, must-revalidate
                [x-rate-limit-limit] => xxx
                [x-rate-limit-remaining] => xxx
                [strict-transport-security] => xxx
                [per-page] => xxx
                [x-xss-protection] => x; xxx
                [x-request-id] => xxx
                [total] => xxx
                [x-frame-options] => xxx
                [x-runtime] => xxx
                [x-content-type-options] => xxx
                [x-rate-limit-reset] => xxx
                [expires] => xxx
                [etag] => xxx
                [status] => 200 OK
                [vary] => Accept-Encoding
                [content-encoding] => gzip
                [content-length] => xxx
                [content-type] => application/json
            )

    )

[body] => [{"abbreviated_name":"","address":"xxx","authorized_bidder":true,"business_phone":"xxx","city":"xxx","company":"xxx","country_code":"xxx","created_at":"2015-07-08T21:16:23Z","email_address":"","fax_number":"xxx","id":xxx,"is_active":true,"labor_union":"","license_number":"","name":"xxx","non_union_prevailing_wage":false,"notes":"","prequalified":false,"state_code":"xxx","union_member":false,"updated_at":"2017-06-07T14:55:50Z","website":"","zip":"xxx","attachments":[],"project_ids":[xxx,xxx,xxx,xxx]},{"abbreviated_name":"","address":"xxx","authorized_bidder":true,"business_phone":"xxx","city":"xxx","company":"xxx","country_code":"xxx","created_at":"2014-04-17T22:58:10Z","email_address":"","fax_number":"xxx","id":xxx,"is_active":true,"labor_union":"","license_number":"xxx","name":"xxx","non_union_prevailing_wage":false,"notes":"","prequalified":false,"state_code":"xxx","union_member":false,"updated_at":"2017-06-07T14:54:58Z","website":"","zip":"85382","attachments":[],"project_ids":[xxx,xxx,xxx,xxx]}]
[response] => Array
    (
        [code] => 200
        [message] => OK
    )

[cookies] => Array
    (
    )

[filename] => 
[http_response] => WP_HTTP_Requests_Response Object
    (
        [response:protected] => Requests_Response Object
            (
                [body] => [{"abbreviated_name":"","address":"xxx","authorized_bidder":true,"business_phone":"xxx","city":"xxx","company":"xxx","country_code":"xxx","created_at":"2015-07-08T21:16:23Z","email_address":"","fax_number":"xxx","id":xxx,"is_active":true,"labor_union":"","license_number":"","name":"xxx","non_union_prevailing_wage":false,"notes":"","prequalified":false,"state_code":"xxx","union_member":false,"updated_at":"2017-06-07T14:55:50Z","website":"","zip":"xxx","attachments":[],"project_ids":[xxx,xxx,xxx,xxx]},{"abbreviated_name":"","address":"xxx","authorized_bidder":true,"business_phone":"xxx","city":"xxx","company":"xxx","country_code":"xxx","created_at":"2014-04-17T22:58:10Z","email_address":"","fax_number":"xxx","id":xxx,"is_active":true,"labor_union":"","license_number":"xxx","name":"xxx","non_union_prevailing_wage":false,"notes":"","prequalified":false,"state_code":"xxx","union_member":false,"updated_at":"2017-06-07T14:54:58Z","website":"","zip":"xxx","attachments":[],"project_ids":[xxx,xxx,xxx,xxx]}]
                [raw] => HTTP/1.1 200 OK
Date: Fri, 09 Jun 2017 21:48:03 GMT
Server:  
Cache-Control: no-store, no-cache, must-revalidate
X-Rate-Limit-Limit: xxx
X-Rate-Limit-Remaining: xxx
Strict-Transport-Security: max-age=xxx
Per-Page: xxx
X-XSS-Protection: 1; mode=block
X-Request-Id: xxx
Total: xxx
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.786317
X-Content-Type-Options: xxx
X-Rate-Limit-Reset: xxx
Expires: xxx
ETag: x/"xxx"
Status: 200 OK
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: xxx
Connection: xxx
Content-Type: application/json

[{"abbreviated_name":"","address":"xxx","authorized_bidder":true,"business_phone":"xxx","city":"xxx","company":"xxx","country_code":"xxx","created_at":"2015-07-08T21:16:23Z","email_address":"","fax_number":"xxx","id":xxx,"is_active":true,"labor_union":"","license_number":"","name":"xxx","non_union_prevailing_wage":false,"notes":"","prequalified":false,"state_code":"xxx","union_member":false,"updated_at":"2017-06-07T14:55:50Z","website":"","zip":"xxx","attachments":[],"project_ids":[xxx,xxx,xxx,xxx]},{"abbreviated_name":"","address":"xxx","authorized_bidder":true,"business_phone":"xxx","city":"xxx","company":"xxx","country_code":"xxx","created_at":"2014-04-17T22:58:10Z","email_address":"","fax_number":"xxx","id":xxx,"is_active":true,"labor_union":"","license_number":"xxx","name":"xxx","non_union_prevailing_wage":false,"notes":"","prequalified":false,"state_code":"xxx","union_member":false,"updated_at":"2017-06-07T14:54:58Z","website":"","zip":"xxx","attachments":[],"project_ids":[xxx,xxx,xxx,xxx]}]
                [headers] => Requests_Response_Headers Object
                    (
                        [data:protected] => Array
                            (
                                [date] => Array
                                    (
                                        [0] => Fri, 09 Jun 2017 21:48:03 GMT
                                    )

                                [server] => Array
                                    (
                                        [0] => 
                                    )

                                [cache-control] => Array
                                    (
                                        [0] => no-store, no-cache, must-revalidate
                                    )

                                [x-rate-limit-limit] => Array
                                    (
                                        [0] => xxx
                                    )

                                [x-rate-limit-remaining] => Array
                                    (
                                        [0] => xxx
                                    )

                                [strict-transport-security] => Array
                                    (
                                        [0] => max-age=xxx
                                    )

                                [per-page] => Array
                                    (
                                        [0] => xxx
                                    )

                                [x-xss-protection] => Array
                                    (
                                        [0] => 1; mode=block
                                    )

                                [x-request-id] => Array
                                    (
                                        [0] => xxx
                                    )

                                [total] => Array
                                    (
                                        [0] => xxx
                                    )

                                [x-frame-options] => Array
                                    (
                                        [0] => xxx
                                    )

                                [x-runtime] => Array
                                    (
                                        [0] => xxx
                                    )

                                [x-content-type-options] => Array
                                    (
                                        [0] => xxx
                                    )

                                [x-rate-limit-reset] => Array
                                    (
                                        [0] => xxx
                                    )

                                [expires] => Array
                                    (
                                        [0] => Thu, 01 Jan 1970 00:00:00 GMT
                                    )

                                [etag] => Array
                                    (
                                        [0] => x/"xxx"
                                    )

                                [status] => Array
                                    (
                                        [0] => 200 OK
                                    )

                                [vary] => Array
                                    (
                                        [0] => Accept-Encoding
                                    )

                                [content-encoding] => Array
                                    (
                                        [0] => xxx
                                    )

                                [content-length] => Array
                                    (
                                        [0] => xxx
                                    )

                                [content-type] => Array
                                    (
                                        [0] => application/json
                                    )

                            )

                    )

                [status_code] => 200
                [protocol_version] => 1.1
                [success] => 1
                [redirects] => 0
                [url] => xxx
                [history] => Array
                    (
                    )

                [cookies] => Requests_Cookie_Jar Object
                    (
                        [cookies:protected] => Array
                            (
                            )

                    )

            )

        [filename:protected] => 
        [data] => 
        [headers] => 
        [status] => 
    )

)

我可以通过使用缩小范围

$response['body']

但这给了我一个用方括号和大括号 [{}] 包裹的字符串。从那里,我不能将它视为 JSON 或没有致命错误的数组。

我想要实现的是缩小范围,以便我可以 foreach 以编程方式将 [body] 中的数据添加到接收站点的数据库条目中。

任何指导将不胜感激!

编辑:我意识到这与此处的其他问题类似,但我发现的现有问题都没有解决 JSON 响应的复杂性,这些响应不是简单的键值对。即使在其他站点上,我也无法找到一个明确的解决方案来解决结合多维数组和 JSON 响应的响应。谢谢您的帮助!

海鸥

你需要使用 json_decode($response['body'])

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章