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

时间:2019-02-17 17:14:05

标签: javascript php jquery 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”:“的列表,其值类似于” slug“:” amici“?

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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)

答案 2 :(得分:0)

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

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