2013-06-05 84 views
1

我有如下因素JSON文件:如何获得JSON属性名PHP

{"ver":2,"sb":[some array],"ld":[some array] ,"hd":[some array]} 

我尝试用下面的代码来GER属性名称:

$path='./datafiles/jsonTest.json'; 
$data = json_decode(file_get_contents($path)); 
$properties=get_object_vars($data); 
foreach($properties as $propName){ 
    echo $propName.'<br>'; 
} 

但结果是我得到:

2 
Array 
Array 
Array 

当我需要时:

'ver' 
'sb' 
'ld' 
'hd' 

有人可以帮助我吗?谢谢!

+1

我看到已经有答案了,但下次您可以使用JSON可视化工具如http://chris.photobooks.com/json/default.htm通过它,您可以轻松查看如何访问所需的字段。 – enenen

回答

2

如果您不需要生成的输出作为一个对象,你coud使用json_decode

$data = json_decode(file_get_contents($path), true); 

$properties = array_keys($data); 
+0

非常感谢! – Braggae

1

你试过了钥匙吗?

foreach($properties as $key => $propName){ 
    echo $key.'<br>'; 
} 
+0

感谢它的作品awsome! – Braggae

3

数组版本,你也可以使用json_decode给你一个关联数组

尝试
$path='./datafiles/jsonTest.json'; 
$data = json_decode(file_get_contents($path),true); 
foreach($data as $name => $value){ 
    echo $name.'<br>'; 
}