JSON 文件怎么读取

whl20080122 2009-03-05 11:06:41
json.txt 文件
{
"programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
]
}


需要读取类别的名字!!
...全文
1393 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
海诗美妆 2009-03-05
  • 打赏
  • 举报
回复
使用json_decode函数
$filename = "json.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
var_dump($contents);
var_dump(json_decode($contents);
bookmoth 2009-03-05
  • 打赏
  • 举报
回复

mixed json_decode ( string $json [, bool $assoc ] )

接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
这是手册里写的。
whl20080122 2009-03-05
  • 打赏
  • 举报
回复
已经解决了,谢谢各位了 !分少了,只有平分了!下次多给点!
yctin 2009-03-05
  • 打赏
  • 举报
回复
另外...你那个json文件格式有错,
    {
"programmers": [{
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "brett@newInstance.com"
} ],
"authors": [{
"firstName": "Isaac",
"lastName": "Asimov",
"genre": "science fiction"
} ],
"musicians": [{
"firstName": "Eric",
"lastName": "Clapton",
"instrument": "guitar"
} ]
}


json.txt 文件
{
"programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
]
}
iasky 2009-03-05
  • 打赏
  • 举报
回复
http://cn2.php.net/manual/en/book.json.php
yctin 2009-03-05
  • 打赏
  • 举报
回复
$json = json_decode(file_get_contents("json.txt"));
echo "<pre>".var_export($json,true)."</pre>";
sunshine_anycall 2009-03-05
  • 打赏
  • 举报
回复
markup

4,250

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧