php 输出JSON格式,使用json_encode函数即可
例如:
header('Content-type: text/json');
$fruits=array(
"fruits"=>array("a"=>"orange","b"=>"banana","c"=>"apple"),
"numbers"=>array(1, 2, 3, 4, 5, 6),
"holes"=>array("first", 5 =>"second","third")
);
echojson_encode($fruits);
?>
得到结果:
{"fruits":{"a":"orange","b":"banana","c":"apple"},"numbers":[1,2,3,4,5,6],"holes":{"0":"first","5":"second","6":"third"}}