drqvsx1228
2017-06-09 23:54
浏览 70
已采纳

在PHP中解析复杂的JSON响应

I apologize in advance if this is a silly question! I've read up on decoding JSON responses, but I can't seem to get this response into a format that's usable.

I'm submitting an GET request via a Wordpress plugin. In order to preview the output so I'd know how to parse it, I used the code

print_r($response, true)

The result I get is:

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] => 
    )

)

I can narrow it down by using

$response['body']

but that gives me a string wrapped in both brackets and braces [{}]. From there, I can't treat it as JSON or an array without fatal errors.

What I'm trying to achieve is narrowing it down so I can foreach to programmatically add the data within [body] into database entries in the receiving site.

Any guidance would be greatly appreciated!!

EDIT: I realize this is similar to other questions on here, but none of the existing questions I found tackled the complexity of JSON responses that are not simple key, value pairs. Even across other sites I was not able to find a clear solution to responses that combined both multidimensional arrays and JSON responses. Thanks for the help!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtao9158 2017-06-09 23:59
    已采纳

    You need to use json_decode($response['body'])

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题