php解析json
关键函数:json_decode(str,true)解析json
json_encode(array()),转换成json
实例如下:(带读写文件的例子)
unlink("a.html");//删除原有文件
$fp=fopen("a.html","a");
$fw=fwrite($fp,json_encode($guanggao_list)); //将guanggao_list数组集成json后存入文件a.html
fclose($fp);
$fp=fopen("a.html","r");
$fw=fread($fp,filesize ("a.html")); //读取文件内容
fclose($fp);
// print_r($fw);
$jsondecode = json_decode($fw,true);//将json解析为数组
print_r($jsondecode);
例子2:
<?php
/*测试如何将数组写入文件*/
$list=array(1=>"a",2=>"b");
if(is_file("ceshi.php"))
{
unlink("ceshi.php");
}
$fo=fopen("ceshi.php","a");
$str=json_encode($list);
if(fwrite($fo,$str)){echo "写入数据成功";}
fclose($fo);
if(is_file("ceshi.php"))
{
$fo=fopen("ceshi.php","r");
$fr=fread($fo,filesize("ceshi.php"));//fr获取的文档的内容
$list=json_decode($fr,true);//json解析后输出的数组
fclose($fo);
}
?>
3.例子3:
<?php
$guanggao_list=array(
0=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img01.taobaocdn.com/bao/uploaded/i1/13692019653157760/T1RedmXvBdXXXXXXXX_!!0-item_pic.jpg_310x310.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
),
1=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1372105283,
"wangwang"=>"小鱼儿"
),
2=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
),
3=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
),
4=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
),
5=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
),
6=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
),
7=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
),
8=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
),
9=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"
)
);
if(is_file("db_json.php"))
{
unlink("db_json.php");
}
$fo=fopen("db_json.php","a");
$str=json_encode($guanggao_list);
if(fwrite($fo,$str)){echo "写入基本数据成功";}
fclose($fo);
if(is_file("db_json.php"))
{
$fo=fopen("db_json.php","r");
$fr=fread($fo,filesize("db_json.php"));//fr获取的文档的内容
$guanggao_list=json_decode($fr,true);//json解析后输出的数组
print_r($guanggao_list);
fclose($fo);
}
?>