如何解码JSON结果以提取密钥名称?

时间:2011-01-17 03:58:42

标签: php json

以下是JSON API调用的结果:

[{"title":"Teqpad.com|| Find Pageviews,Visits,Visitors,Unique Visitors,Website Worth...","url":"http:\/\/www.teqpad.com\/","top_tags":{"analytics":64,"statistics":57,"tools":43,"seo":41,"traffic":40,"search":22,"web":20,"blog":18,"blogging":14,"website":13}}]

我想仅提取top_tags例如:

analytics,statistics,tools,seo,traffic,search

如何使用json_decode()

执行此操作

2 个答案:

答案 0 :(得分:2)

您的JSON格式错误,top_tags键引用了格式错误的对象(键,但没有值) - 它应该引用一个数组,如下所示:

[{"title":"Teqpad.com|| Find Pageviews,Visits,Visitors,Unique Visitors,Website  Worth...","url":"http:\/\/www.teqpad.com\/","top_tags":["analytics","statistics","tools","seo","traffic","search","web","blog","blogging","website"]}

然后使用以下代码解析它:

$json = ...
$obj = json_decode($json);
$top_tags = $obj[0]->top_tags;
print_r($top_tags);

Codepad example

答案 1 :(得分:0)

$obj = json_decode($json, true);
$top_tags = array_keys($obj[0]['top_tags']);
print_r($top_tags);

工作示例:http://codepad.org/QWjQQ097