生成JSON格式
<?phpheader(‘content-type:application/json’);$number = array('1','2','3','4','5','6','7','8','9','0');echo json_encode($number); //json_encode对变量进行 JSON 编码?>
JSON格式的解析
<?phpheader(‘content-type:application/json’);$number = array('1','2','3','4','5','6','7','8','9','0');echo json_encode($number); //json_encode对变量进行 JSON 编码echo json_decode($number); //对 JSON 格式的字符串进行解码,转换为 PHP 变量?>
header(‘Content-type:text/json’);
只是http协议中标识出,输出是什么类型的内容。
实际使用中,有时随然此值,不是text/json 浏览器依然可以正常解析成为json格式,只是因为浏览器端,做了json格式内容的自动识别,或者有浏览器插件,做了自动识别。