php操作JSON格式數(shù)據(jù)的實(shí)現(xiàn)代碼
更新時(shí)間:2011年12月24日 03:24:10 作者:
php操作JSON格式數(shù)據(jù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下。
知識(shí)點(diǎn):
1、JSON數(shù)據(jù)格式介紹
2、對數(shù)據(jù)編碼成JSON格式
3、對JSON數(shù)據(jù)進(jìn)行解碼,并操作
JSON數(shù)據(jù)格式表示方式如下:
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
用php將數(shù)據(jù)編碼成JSON格式:
<?php
//php中用數(shù)組表示JSON格式數(shù)據(jù)
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', '非誠'),
'lastname' => iconv('gb2312', 'utf-8', '勿擾'),
'contact' => array(
'email' =>'fcwr@jb51.net',
'website' =>'http://www.dbjr.com.cn',
)
);
//將數(shù)組編碼成JSON數(shù)據(jù)格式
$json_string = json_encode($arr);
//JSON格式數(shù)據(jù)可直接輸出
echo $json_string;
?>
需要指出的是,在非UTF-8編碼下,中文字符將不可被encode,結(jié)果會(huì)出來空值,所以,如果你使用 gb2312編寫PHP代碼,那么就需要將包含中文的內(nèi)容使用iconv或者mb轉(zhuǎn)為UTF-8再進(jìn)行json_encode。
輸出:(JSON格式)
{"firstname":"\u975e\u8bda","lastname":"\u52ff\u6270","contact":{"email":"fcwr@jb51.net","website":"http:\/\/www.dbjr.com.cn"}}
用php對JSON數(shù)據(jù)進(jìn)行解碼并處理:
<?php
//php中用數(shù)組表示JSON格式數(shù)據(jù)
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', '非誠'),
'lastname' => iconv('gb2312', 'utf-8', '勿擾'),
'contact' => array(
'email' =>'fcwr@jb51.net',
'website' =>'http://www.dbjr.com.cn',
)
);
//將數(shù)組編碼成JSON數(shù)據(jù)格式
$json_string = json_encode($arr);
//將JSON格式數(shù)據(jù)進(jìn)行解碼,解碼后不是JSON數(shù)據(jù)格式,不可用echo直接輸出
$obj = json_decode($json_string);
//強(qiáng)制轉(zhuǎn)化為數(shù)組格式
$arr = (array) $obj;
//按數(shù)組方式調(diào)用里面的數(shù)據(jù)
echo iconv('utf-8','gb2312',$arr['firstname']);
echo '</br>';
//輸出數(shù)組結(jié)構(gòu)
print_r($arr);
?>
輸出:
非誠
Array ( [firstname] => 闈炶瘹 [lastname] => 鍕挎壈 [contact] => stdClass Object ( [email] => fcwr@jb51.net [website] => http://www.dbjr.com.cn ) )
1、JSON數(shù)據(jù)格式介紹
2、對數(shù)據(jù)編碼成JSON格式
3、對JSON數(shù)據(jù)進(jìn)行解碼,并操作
JSON數(shù)據(jù)格式表示方式如下:
復(fù)制代碼 代碼如下:
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
用php將數(shù)據(jù)編碼成JSON格式:
復(fù)制代碼 代碼如下:
<?php
//php中用數(shù)組表示JSON格式數(shù)據(jù)
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', '非誠'),
'lastname' => iconv('gb2312', 'utf-8', '勿擾'),
'contact' => array(
'email' =>'fcwr@jb51.net',
'website' =>'http://www.dbjr.com.cn',
)
);
//將數(shù)組編碼成JSON數(shù)據(jù)格式
$json_string = json_encode($arr);
//JSON格式數(shù)據(jù)可直接輸出
echo $json_string;
?>
需要指出的是,在非UTF-8編碼下,中文字符將不可被encode,結(jié)果會(huì)出來空值,所以,如果你使用 gb2312編寫PHP代碼,那么就需要將包含中文的內(nèi)容使用iconv或者mb轉(zhuǎn)為UTF-8再進(jìn)行json_encode。
輸出:(JSON格式)
{"firstname":"\u975e\u8bda","lastname":"\u52ff\u6270","contact":{"email":"fcwr@jb51.net","website":"http:\/\/www.dbjr.com.cn"}}
用php對JSON數(shù)據(jù)進(jìn)行解碼并處理:
復(fù)制代碼 代碼如下:
<?php
//php中用數(shù)組表示JSON格式數(shù)據(jù)
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', '非誠'),
'lastname' => iconv('gb2312', 'utf-8', '勿擾'),
'contact' => array(
'email' =>'fcwr@jb51.net',
'website' =>'http://www.dbjr.com.cn',
)
);
//將數(shù)組編碼成JSON數(shù)據(jù)格式
$json_string = json_encode($arr);
//將JSON格式數(shù)據(jù)進(jìn)行解碼,解碼后不是JSON數(shù)據(jù)格式,不可用echo直接輸出
$obj = json_decode($json_string);
//強(qiáng)制轉(zhuǎn)化為數(shù)組格式
$arr = (array) $obj;
//按數(shù)組方式調(diào)用里面的數(shù)據(jù)
echo iconv('utf-8','gb2312',$arr['firstname']);
echo '</br>';
//輸出數(shù)組結(jié)構(gòu)
print_r($arr);
?>
輸出:
非誠
Array ( [firstname] => 闈炶瘹 [lastname] => 鍕挎壈 [contact] => stdClass Object ( [email] => fcwr@jb51.net [website] => http://www.dbjr.com.cn ) )
相關(guān)文章
php數(shù)組函數(shù)序列之a(chǎn)rray_values() 獲取數(shù)組元素值的函數(shù)與方法
array_keys() 函數(shù)返回包含數(shù)組中所有鍵名的一個(gè)新數(shù)組。如果提供了第二個(gè)參數(shù),則只返回鍵值為該值的鍵名2011-10-10PHP將DateTime對象轉(zhuǎn)化為友好時(shí)間顯示的實(shí)現(xiàn)代碼
PHP將DateTime對象轉(zhuǎn)化為友好時(shí)間顯示的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-09-09php監(jiān)測數(shù)據(jù)是否成功插入到Mysql數(shù)據(jù)庫的方法
在PHP中該怎么用代碼判斷數(shù)據(jù)是否成功插入到Mysql數(shù)據(jù)庫?這篇文章中給出了兩種解決的方法,大家可以根據(jù)需要選擇相對應(yīng)的方法,有需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11PHP+Ajax實(shí)時(shí)自動(dòng)檢測是否聯(lián)網(wǎng)的方法
這篇文章主要介紹了PHP+Ajax實(shí)時(shí)自動(dòng)檢測是否聯(lián)網(wǎng)的方法,通過Ajax調(diào)用連接百度效果實(shí)現(xiàn)檢測網(wǎng)站是否聯(lián)網(wǎng)的功能,需要的朋友可以參考下2015-07-07PHP設(shè)計(jì)模式之建造者模式定義與用法簡單示例
這篇文章主要介紹了PHP設(shè)計(jì)模式之建造者模式定義與用法,簡單描述了建造者模式的概念、原理并結(jié)合實(shí)例形式分析了建造者模式的具體定義與使用方法,需要的朋友可以參考下2018-08-08