【发布时间】:2014-04-07 20:50:34
【问题描述】:
如何在php中解析下面的json
[
{ "user":"John", "age":22, "country":"United States" },
{ "user":"Will", "age":27, "country":"United Kingdom" },
{ "user":"Abiel", "age":19, "country":"Mexico" },
{ "user":"Rick", "age":34, "country":"Panama" },
{ "user":"Susan", "age":23, "country":"Germany" },
{ "user":"Amy", "age":43, "country":"France" }
]
我使用以下代码,但它没有工作
$jsonData = file_get_contents("http://localhost/attendance1/a.json");
$phpArray = json_decode($jsonData, true);
echo $phpArray;
foreach ($phpArray as $key => $value) {
echo "<h2>$key</h2>";
foreach ($value as $k => $v) {
echo "$k | $v <br />";
}
}
【问题讨论】:
-
这不是有效的 JSON。
-
数组应该用
[ ... ]包围,而不是{ ... }。 -
这不是一个有效的 JSON 字符串。
-
使用更正后的 JSON,您的脚本可以为我工作。
-
现在可以工作了..谢谢