如何在json中获取特定对象?

时间:2019-03-01 12:32:27

标签: php

我有以下回应:

[  
   {  
      "id":128412,
      "date":"2019-02-20T09:20:26",
      "date_gmt":"2019-02-20T09:20:26",
      "guid":{  
         "rendered":"https:\/\/www.example.it\/francesca\/"
      },
      "modified":"2019-02-20T09:20:26",
      "modified_gmt":"2019-02-20T09:20:26",
      "slug":"francesca",
      "status":"publish",
      "type":"post",
      "link":"https:\/\/www.example.it\/francesca\/",
      "title":{  
         "rendered":"Francesca"
      },
      "content":{  
         "rendered":"<p>Ero uscito per una passeggiata con il mio cane,<br \/>e ho incontrato Francesca e Machete.<br \/>Oggi siamo una famiglia \ud83d\ude42<\/p>\n",
         "protected":false
      },
      "excerpt":{  
         "rendered":"<p class=\"card-text dark\">Ero uscito per una passeggiata con il mio cane,e ho incontrato Francesca [&hellip;]<\/p>\n",
         "protected":false
      },
      "author":55,
      "featured_media":128413,
      "comment_status":"closed",
      "ping_status":"open",
      "sticky":false,
      "template":"",
      "format":"standard",
      "meta":[  

      ],
      "categories":[  
         139,
         749,
         506
      ],
      "tags":[  
         750,
         745,
         643
      ],
      "usp-custom-90":"45.5013777661607,9.21768474999999",
      "_links":{  
         "self":[  
            {  
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128412"
            }
         ],
         "collection":[  
            {  
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts"
            }
         ],
         "about":[  
            {  
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/types\/post"
            }
         ],
         "author":[  
            {  
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/users\/55"
            }
         ],
         "replies":[  
            {  
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/comments?post=128412"
            }
         ],
         "version-history":[  
            {  
               "count":0,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128412\/revisions"
            }
         ],
         "wp:featuredmedia":[  
            {  
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media\/128413"
            }
         ],
         "wp:attachment":[  
            {  
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media?parent=128412"
            }
         ],
         "wp:term":[  
            {  
               "taxonomy":"category",
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/categories?post=128412"
            },
            {  
               "taxonomy":"post_tag",
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/tags?post=128412"
            }
         ],
         "curies":[  
            {  
               "name":"wp",
               "href":"https:\/\/api.w.org\/{rel}",
               "templated":true
            }
         ]
      },
      "_embedded":{  
         "author":[  
            {  
               "id":55,
               "name":"Antonio Forina",
               "url":"",
               "description":"",
               "link":"https:\/\/www.example.it\/author\/antonio\/",
               "slug":"antonio",
               "avatar_urls":{  
                  "24":"https:\/\/secure.gravatar.com\/avatar\/7cab78abf9b71cd25f5660d4dbbe9d25?s=24&d=mm&r=g",
                  "48":"https:\/\/secure.gravatar.com\/avatar\/7cab78abf9b71cd25f5660d4dbbe9d25?s=48&d=mm&r=g",
                  "96":"https:\/\/secure.gravatar.com\/avatar\/7cab78abf9b71cd25f5660d4dbbe9d25?s=96&d=mm&r=g"
               },
               "_links":{  
                  "self":[  
                     {  
                        "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/users\/55"
                     }
                  ],
                  "collection":[  
                     {  
                        "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/users"
                     }
                  ]
               }
            }
         ],
         "wp:featuredmedia":[  
            {  
               "id":128413,
               "date":"2019-02-20T09:20:26",
               "slug":"2019-02-20_5c6d1bdabff9a_tanzballerina-1281",
               "type":"attachment",
               "link":"https:\/\/www.example.it\/francesca\/2019-02-20_5c6d1bdabff9a_tanzballerina-1281\/",
               "title":{  
                  "rendered":"2019-02-20_5c6d1bdabff9a_tanzballerina-1281"
               },
               "author":55,
               "caption":{  
                  "rendered":"<p class=\"card-text dark\">\n"
               },
               "alt_text":"",
               "media_type":"image",
               "mime_type":"image\/jpeg",
               "media_details":{  
                  "width":640,
                  "height":427,
                  "file":"2019\/02\/2019-02-20_5c6d1bdabff9a_tanzballerina-1281.jpeg",
                  "sizes":{  
                     "thumbnail":{  
                        "file":"2019-02-20_5c6d1bdabff9a_tanzballerina-1281-150x150.jpeg",
                        "width":"150",
                        "height":"150",
                        "mime_type":"image\/jpeg",
                        "source_url":"https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-20_5c6d1bdabff9a_tanzballerina-1281-150x150.jpeg"
                     },
                     "medium":{  
                        "file":"2019-02-20_5c6d1bdabff9a_tanzballerina-1281-280x187.jpeg",
                        "width":"280",
                        "height":"187",
                        "mime_type":"image\/jpeg",
                        "source_url":"https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-20_5c6d1bdabff9a_tanzballerina-1281-280x187.jpeg"
                     },
                     "alm-thumbnail":{  
                        "file":"2019-02-20_5c6d1bdabff9a_tanzballerina-1281-150x150.jpeg",
                        "width":"150",
                        "height":"150",
                        "mime_type":"image\/jpeg",
                        "source_url":"https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-20_5c6d1bdabff9a_tanzballerina-1281-150x150.jpeg"
                     },
                     "full":{  
                        "file":"2019-02-20_5c6d1bdabff9a_tanzballerina-1281.jpeg",
                        "width":640,
                        "height":427,
                        "mime_type":"image\/jpeg",
                        "source_url":"https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-20_5c6d1bdabff9a_tanzballerina-1281.jpeg"
                     }
                  },
                  "image_meta":{  
                     "aperture":"7.1",
                     "credit":"",
                     "camera":"NIKON D5200",
                     "caption":"",
                     "created_timestamp":"1480250162",
                     "copyright":"",
                     "focal_length":"52",
                     "iso":"200",
                     "shutter_speed":"0.00625",
                     "title":"",
                     "orientation":"0"
                  }
               },
               "source_url":"https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-20_5c6d1bdabff9a_tanzballerina-1281.jpeg",
               "_links":{  
                  "self":[  
                     {  
                        "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media\/128413"
                     }
                  ],
                  "collection":[  
                     {  
                        "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media"
                     }
                  ],
                  "about":[  
                     {  
                        "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/types\/attachment"
                     }
                  ],
                  "author":[  
                     {  
                        "embeddable":true,
                        "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/users\/55"
                     }
                  ],
                  "replies":[  
                     {  
                        "embeddable":true,
                        "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/comments?post=128413"
                     }
                  ]
               }
            }
         ],

2 个答案:

答案 0 :(得分:4)

代替

$content->{"_embedded"}->{"wp:featuredmedia"}->sizes->thumbnail->{"source_url"}

尝试

$content['_embedded']['wp:featuredmedia'][0]['media_details']['sizes']['thumbnail']['source_url'];

答案 1 :(得分:1)

为json_decode()的第二个参数提供true会使该函数返回一个关联数组,因此您尝试的第一个代码将不起作用,第二个是正确的。

根据您的数据,应该是$content['_embedded']['wp:featuredmedia'][0]['media_details']['sizes']['thumbnail']['source_url']之类的东西。

在任何情况下,您都可以对$ contents变量执行var_dump()以确保对象的结构符合您的期望。