简体   繁体   中英

How to navigate and get a specific json object in URL?

I getting a json from a url but it has lots of objects, how would I only get the slugs giving the json provided below?

I don't mind it either in php or js, I mean on the one hand I need to know how to get what object I want using php or js, on the other hand I am wondering if we can get an object providing a parameter in the url

[  
   {  
      "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":[  

How would I get a list of all "slug":" with their value like "slug":"amici" in a url?

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

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

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

You can use map and destructuring assignment

 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) 

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

$slugs = array_column($posts, 'slug');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM