获取JSON的字段名称

时间:2015-01-26 00:25:05

标签: php json

我有一个包含服务器列表的JSON:

 {
    "serverlist": {
        "live": {
            "Echo server": {
                "age": "00:05:39",
                "title": "sometitle"
            },
            "Rio server": {
                "age": "00:02:57",
                "title": "sometitle"
            },
            "Goodies server": {
                "age": "00:04:14",
                "title": "sometitle"
            },
            "Ben server": {
                "age": "00:03:30",
                "title": "sometitle"
            }
        }
    }
}

我有这段代码:

$json_string = file_get_contents("jsonurl");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['serverlist']['live'];

foreach($parsed_json as $key => $value)
{
   echo $value['age'] . '<br>';
   echo $value['tist'] . '<br>';
}

我试图获取每个服务器名称(echo服务器,rio服务器等)。 我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

服务器名称是密钥,因此它位于$key变量中。

foreach($parsed_json as $key => $value)
{
   echo $key . '<br>';
   echo $value['age'] . '<br>';
   echo $value['tist'] . '<br>';
}