首页 > 解决方案 > 如何在 URL 中导航和获取特定的 json 对象?

问题描述

我从一个 url 得到一个 json,但它有很多对象,我怎么才能只得到下面提供的 json 的蛞蝓?

我不介意在 php 或 js 中,我的意思是一方面我需要知道如何使用 php 或 js 获取我想要的对象,另一方面我想知道我们是否可以获得提供参数的对象在网址中

[  
   {  
      "id":580,
      "count":0,
      "description":"qwer",
      "link":"http:\/\/example.com\/ing\/category\/persone\/414qwer1324r-qewr1233423\/",
      "name":"414qwer1324r qewr1233423",
      "slug":"414qwer1324r-qewr1233423",
      "taxonomy":"category",
      "parent":183,
      "meta":[  

      ],
      "_links":{  
         "self":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/580"
            }
         ],
         "collection":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories"
            }
         ],
         "about":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/taxonomies\/category"
            }
         ],
         "up":[  
            {  
               "embeddable":true,
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/183"
            }
         ],
         "wp:post_type":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/posts?categories=580"
            }
         ],
         "curies":[  
            {  
               "name":"wp",
               "href":"https:\/\/api.w.org\/{rel}",
               "templated":true
            }
         ]
      }
   },
   {  
      "id":586,
      "count":1,
      "description":"asdfasd",
      "link":"http:\/\/example.com\/ing\/category\/persone\/add-person\/",
      "name":"Add person",
      "slug":"add-person",
      "taxonomy":"category",
      "parent":183,
      "meta":[  

      ],
      "_links":{  
         "self":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/586"
            }
         ],
         "collection":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories"
            }
         ],
         "about":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/taxonomies\/category"
            }
         ],
         "up":[  
            {  
               "embeddable":true,
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/183"
            }
         ],
         "wp:post_type":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/posts?categories=586"
            }
         ],
         "curies":[  
            {  
               "name":"wp",
               "href":"https:\/\/api.w.org\/{rel}",
               "templated":true
            }
         ]
      }
   },
   {  
      "id":520,
      "count":8,
      "description":"",
      "link":"http:\/\/example.com\/ing\/category\/argomenti\/amici\/",
      "name":"AMICI",
      "slug":"amici",
      "taxonomy":"category",
      "parent":184,
      "meta":[  

我将如何获得所有“slug”的列表:”它们的值如 url 中的“slug”:“amici”?

喜欢http://example.com/ing/wp-json/wp/v2/categories/names

标签: javascriptphpjqueryjson

解决方案


在 JavaScript 中,您可以使用Array.prototype.map()和解构赋值

let res = data.map(({slug}) => slug)

推荐阅读