如何使用PHP解析URL中的JSON

时间:2015-05-29 14:48:48

标签: php json parsing

我有这个网址:

http://server.com/api.php?results

{"Name":"Pit, Loka","Current":{"Item":"16","test":"test","test":"84","test":"ok"}}

我猜PHP示例是:

<?php
$file = file_get_contents("http://server.com/api.php?results=item");
$data = json_decode($file);
print_r($data);
?>

但我无法获得任何结果 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

<?php
$file = file_get_contents("http://server.com/api.php?results=item");
$data = json_decode($file);

$name = $data->Name; // "Name":"Pit, Loka"
$current = $data->Current->Item; //"Current":{"Item":"16","test":"test","test":"84","test":"ok"}

foreach ($current as $currentX) {
    echo $currentX->Item;
}
?>

它应该工作......请测试。