首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用PHP解析复杂的JSON响应

用PHP解析复杂的JSON响应
EN

Stack Overflow用户
提问于 2017-06-09 23:54:11
回答 1查看 815关注 0票数 0

如果这是个愚蠢的问题,我提前道歉!我读过关于解码JSON响应的文章,但是我似乎无法将这个响应转换成一种可用的格式。

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

代码语言:javascript
复制
print_r($response, true)

我得到的结果是:

代码语言:javascript
复制
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] => 
    )

)

我可以用

代码语言:javascript
复制
$response['body']

但这给了我一个包含在括号和大括号{}中的字符串。在那里,我不能将它视为JSON或数组,而不会有致命的错误。

我试图实现的是缩小范围,以便可以编程地将正文中的数据添加到接收站点中的数据库条目中。

如有任何指导,将不胜感激!

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-09 23:59:09

您需要使用json_decode($response‘’body‘)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44468109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档