在PHP中解析复杂的JSON响应

时间:2017-06-09 23:54:11

标签: php arrays json wordpress parsing

如果这是一个愚蠢的问题我提前道歉!我已经阅读了解码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或没有致命错误的数组。

我正在努力实现的目标是缩小范围,以便我能够以编程方式将[body]中的数据添加到接收站点的数据库条目中。

非常感谢任何指导!!

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

1 个答案:

答案 0 :(得分:1)

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