php操作JSON格式數(shù)據(jù)的實現(xiàn)代碼
更新時間:2011年12月24日 03:24:10 作者:
php操作JSON格式數(shù)據(jù)的實現(xiàn)代碼,需要的朋友可以參考下。
知識點:
1、JSON數(shù)據(jù)格式介紹
2、對數(shù)據(jù)編碼成JSON格式
3、對JSON數(shù)據(jù)進行解碼,并操作
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://m.fzitv.net',
)
);
//將數(shù)組編碼成JSON數(shù)據(jù)格式
$json_string = json_encode($arr);
//JSON格式數(shù)據(jù)可直接輸出
echo $json_string;
?>
需要指出的是,在非UTF-8編碼下,中文字符將不可被encode,結(jié)果會出來空值,所以,如果你使用 gb2312編寫PHP代碼,那么就需要將包含中文的內(nèi)容使用iconv或者mb轉(zhuǎn)為UTF-8再進行json_encode。
輸出:(JSON格式)
{"firstname":"\u975e\u8bda","lastname":"\u52ff\u6270","contact":{"email":"fcwr@jb51.net","website":"http:\/\/m.fzitv.net"}}
用php對JSON數(shù)據(jù)進行解碼并處理:
<?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://m.fzitv.net',
)
);
//將數(shù)組編碼成JSON數(shù)據(jù)格式
$json_string = json_encode($arr);
//將JSON格式數(shù)據(jù)進行解碼,解碼后不是JSON數(shù)據(jù)格式,不可用echo直接輸出
$obj = json_decode($json_string);
//強制轉(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://m.fzitv.net ) )
1、JSON數(shù)據(jù)格式介紹
2、對數(shù)據(jù)編碼成JSON格式
3、對JSON數(shù)據(jù)進行解碼,并操作
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://m.fzitv.net',
)
);
//將數(shù)組編碼成JSON數(shù)據(jù)格式
$json_string = json_encode($arr);
//JSON格式數(shù)據(jù)可直接輸出
echo $json_string;
?>
需要指出的是,在非UTF-8編碼下,中文字符將不可被encode,結(jié)果會出來空值,所以,如果你使用 gb2312編寫PHP代碼,那么就需要將包含中文的內(nèi)容使用iconv或者mb轉(zhuǎn)為UTF-8再進行json_encode。
輸出:(JSON格式)
{"firstname":"\u975e\u8bda","lastname":"\u52ff\u6270","contact":{"email":"fcwr@jb51.net","website":"http:\/\/m.fzitv.net"}}
用php對JSON數(shù)據(jù)進行解碼并處理:
復制代碼 代碼如下:
<?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://m.fzitv.net',
)
);
//將數(shù)組編碼成JSON數(shù)據(jù)格式
$json_string = json_encode($arr);
//將JSON格式數(shù)據(jù)進行解碼,解碼后不是JSON數(shù)據(jù)格式,不可用echo直接輸出
$obj = json_decode($json_string);
//強制轉(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://m.fzitv.net ) )
相關(guān)文章
php文件操作小結(jié)(刪除指定文件/獲取文件夾下的文件名/讀取文件夾下圖片名)
這篇文章主要介紹了php文件操作方法,包括刪除指定文件、獲取文件夾下的文件名及讀取文件夾下圖片名,涉及php針對文件與目錄的常用操作技巧,需要的朋友可以參考下2016-05-05
批量獲取memcache值并按key的順序返回的實現(xiàn)代碼
一臺memcache時返回是正確的,在有多臺memcache時就無法一一對應的按順序返回。2011-06-06

