如何在URL中导航并获取特定的json对象?

问题描述 投票:0回答:3

我从一个网址获得一个json,但它有很多对象,我怎么才能得到下面提供json的slu ??

我不介意它在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”的列表:“它们的价值如”slug“:”amici“在一个网址中?

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

javascript php jquery json
3个回答
1
投票

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

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

1
投票

您可以使用地图和解构分配

let data = [{"id":580,"count":0,"description":"qwer","link":"http:\/\/example.com\/ing\/category\/persone\/414qwer1324r-qewr1233423\/","name":"414qwer1324rqewr1233423","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":"Addperson","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}]}}]

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

console.log(op)

0
投票

在PHP中,你可以使用array_column函数:

$slugs = array_column($posts, 'slug');
© www.soinside.com 2019 - 2024. All rights reserved.