首頁 >後端開發 >php教程 >對比json_encode和json_decode的區別

對比json_encode和json_decode的區別

巴扎黑
巴扎黑原創
2017-07-20 17:22:442144瀏覽

JSON的四個基本準則

1)並列的資料之間用逗號(,)分割
2)映射用(":")冒號表示
3)並列資料的集合(資料)用方括號("[]")表示
4)映射的集合(物件)以大括號("{}")表示

1.json_decode() 

json_decode 
(PHP 5 >= 5.2.0, PECL json >= 1.2.0) 

#json_decode — 對JSON 格式的字元串進行編碼 

說明 
mixed json_decode ( string $json [, bool $assoc ] ) 
接受一個JSON 格式的字串並且把它轉換為PHP 變數 

參數參數參數

json 
待解碼的json string 格式的字串。 

assoc 
當此參數為 TRUE 時,將傳回 array 而非 object 。


回傳值 
Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned. 

範例#Example #1
Example #1 () 的範例 

複製程式碼如下:

$json = '{"a":1,"b":2 ,"c":3,"d":4,"e":5}'; 
var_dump(json_decode($json)); 
var_dump(json_decode($json, true)); 
# ?> 

上例輸出: 

#複製程式碼如下:

##object(stdClass)#1 ( 5) { 

["a"] => int(1) 
["b"] => int(2) 
["c"] => int(3) 
["d"] => int(4) 
["e"] => int(5) 


array(5) { 
["a" ] => int(1) 
["b"] => int(2) 
["c"] => int(3) 
["d"] => int (4) 
["e"] => int(5) 




複製程式碼如下:

#$data='[{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1" ,"Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000", "QQNo":""}]'; 

echo json_decode($data); 


結果為: 



複製程式碼如下:

Array ( [0] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) ) 



可以看出經過json_decode()編譯出來的是物件,現在輸出json_decode ($data,true)試下 



複製程式碼如下:

echo json_decode($data,true); 




##結果: 

複製程式碼如下:


Array ( [0] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) ) 


可以看出json_decode($data,true)輸出的一個關聯數組,由此可知json_decode($data)輸出的是物件,而json_decode("$arr",true)是把它強制產生PHP關聯數組. 

2.json_encode() 

json_encode 
(PHP 5 >= 5.2.0, PECL json >= 1.2.0) 

json_encode — 對變數進行JSON編碼 

Report a bug 說明 
string json_encode ( mixed $value [, int $options = 0 ] ) 
傳回value 值的JSON 形式 
##Report a bug 參數參數











44標記
#value 
待編碼的value ,除了resource 類型之外,可以為任何資料類型 

此函數只能接受UTF-8 編碼的資料 

#options 
由以下常數組成的二進位遮罩: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES_SON_SON_FbugCE_JJECT) ##編碼成功則回傳一個以JSON 形式表示的string 或在失敗時傳回FALSE 。 

Report a bug 更新日誌 
版本 說明 
5.4.0 options 參數增加常數: JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, 和 JSON_UNESCAPED_UNICODE。 
5.3.3 options 參數增加常數:JSON_NUMERIC_CHECK。

5.3.0 增加options 參數. 

###Report a bug 範例 ######Example #1 A json_encode() 的範例 ##########複製程式碼如下:###


$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4, 'e'=>5); 

echo json_encode($arr); 
?> 


以上例程會輸出: 

複製程式碼如下:


{"a":1,"b":2,"c":3,"d":4,"e":5}


Example #2 json_encode() 函數中options 參數的用法 

複製程式碼如下:


#$a = array('',"'bar'",'"baz"','&blong&', "\xc3\xa9"); 

echo "Normal: ", json_encode($a), "\n"; 
echo "Tags: ", json_encode($a, JSON_HEX_TAG), "\n"; 
echo "Apos: ", json_encode($a, JSON_HEX_APOS##echo "Apos: ", json_encode($a, JSON_HEX_APOS ), "\n"; 
echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "\n"; 
echo "Amp: ", json_encode($a, JSON_HEX_AMP), "\n" ; 
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n"; 
echo "All: ", json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS:JSON_HEXF, JSON_HEX_QU_DEJSON_HEX_APOS n\n"; 

$b = array(); 

echo "Empty array output as array: ", json_encode($b), "\n"; 
echo " Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n"; 

$c = array(array(1,2,3)); 

echo "Non-associative array output as array: ", json_encode($c), "\n"; 
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT, "n\n\ECTn\n\n\ n"; 

$d = array('foo' => 'bar', 'baz' => 'long'); 

echo "Associative array always output as object: ", json_encode($d), "\n"; 
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n"; ##?> ##?> 

以上程式會輸出: 

複製程式碼如下:

Normal: [""," 'bar'","\"baz\"","&blong&","\u00e9"] 
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\""," &blong&","\u00e9"] 
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"] 
Quot 
Quot ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"] 
Amp: ["","'bar'"," \"baz\"","\u0026blong\u0026","\u00e9"] 
Unicode: ["","'bar'","\"baz\"","&blong&", "é"] 
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"] ##arra#Empty output as array: [] 
Empty array output as object: {} 

Non-associative array output as array: [[1,2,3]] 
Non-associative array output as object : {"0":{"0":1,"1":2,"2":3}} 

Associative array always output as object: {"foo":"bar","baz ":"long"} 
Associative array always output as object: {"foo":"bar","baz":"long"} 


#Example #3 連續與非連續陣列範例 



複製程式碼如下:

echo "連續陣列".PHP_EOL; 
$sequential = array( "foo", "bar", "baz", "blong"); 
var_dump( 
$sequential, 
json_encode($sequential) 
); 

echo PHP_EOL. "非連續陣列".PHP_EOL; 
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong"); 
var_dump( 
$nonsequential, 
json_encode($nonsequential) 
); 

echo PHP_EOL."刪除一個連續數組值的方式產生的非連續數組".PHP_EOL; 
unset($sequential[1]); 
var_dump( 
$sequential, 
json_encode($sequential) 
); 
?> 




#######################################################1> ###以上例程會輸出: #########複製程式碼程式碼如下:###


連續陣列 
array(4) { 
[0]=> 
string(3) "foo" 
[1]=> 
string(3 ) "bar" 
[2]=> 
string(3) "baz" 
[3]=> 
string(5) "blong" 

string (27) "["foo","bar","baz","blong"]" 

非連續陣列 
array(4) { 
[1]=> 
string(3) "foo" 
[2]=> 
string(3) "bar" 
[3]=> 
string(3) "baz" 
#[ 4]=> 
string(5) "blong" 

string(43) "{"1":"foo","2":"bar","3":"baz ","4":"blong"}" 

刪除一個連續數組值的方式所產生的非連續數組 
array(3) { 
[0]=> 
string (3) "foo" 
[2]=> 
string(3) "baz" 
[3]=> 
string(5) "blong" 

string(33) "{"0":"foo","2":"baz","3":"blong"}" 


#複製碼代碼如下:


$obj->Name= 'a1';$obj->Number ='123'; 
$obj->Contno= '000';
echo json_encode($obj); 


結果為: 

複製程式碼如下:


{" Name":"a1", 
"Number":"123", 
"Contno":"000" 


#可以看出json_encode()和json_decode()是編譯和反編譯過程,注意json只接受utf-8編碼的字符,所以json_encode()的參數必須是utf-8編碼,否則會得到空字符或null。


json 
待解碼的 json string 格式的字串。 

assoc 
當此參數為 TRUE 時,將傳回 array 而非 object 。


回傳值 
Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned. 

範例#Example #1
Example #1 () 的範例 

複製程式碼如下:

$json = '{"a":1,"b":2 ,"c":3,"d":4,"e":5}'; 
var_dump(json_decode($json)); 
var_dump(json_decode($json, true)); 
# ?> 

上例輸出: 

複製程式碼如下:

object(stdClass)#1 (5) { 
["a"] => int(1) 
["b"] => int(2) 
["c"] => int(3) 
#["d"] => int(4) 
["e"] => int(5) 


array(5) { 
["a"] => int( 1) 
["b"] => int(2) 
["c"] => int(3) 
["d"] => int(4) 
# ["e"] => int(5) 



#複製程式碼如下:

##$data='[{"Name ":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno" :"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""}]'; 

echo json_decode($data); 


結果為: 


複製程式碼如下:

##Array ( [0] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => stdClass Object ( [Name] => a1 [Number] => ; 123 [Contno] => 000 [QQNo] => ) [2] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] = > ) ) 


可以看出經過json_decode()編譯出來的是物件,現在輸出json_decode($data,true)試下 

複製程式碼如下:

echo json_decode($data,true); 


#結果: 

複製程式碼如下:

Array ( [ 0] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) ) 


可以看出json_decode($data,true)輸出的一個關聯數組,由此可知json_decode($data)輸出的是物件,而json_decode("$arr",true)是把它強制產生PHP關聯數組. 

2.json_encode() 

json_encode 
(PHP 5 >= 5.2.0, PECL json >= 1.2.0) 

#json_encode — 對變數進行JSON 編碼 

Report a bug 說明 
string json_encode ( mixed $value [, int $options = 0 ] ) 
傳回value 值的JSON 形式 
#bug#Report a bug參數 

value 
待編碼的value ,除了resource 類型之外,可以為任何資料型別 

此函數只能接受UTF-8 編碼的資料 

options 
由以下常數組成的二進位遮罩: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UFCAP_U32J3EDFFREFREFREFREF4 C5_FFREFREF), J57_FREFREFREFREF), 167_FFREFREFF), C5,J5_UFREFREFREFREFREFREFFFF) C5_FF)7. bug 回傳值 
編碼成功則回傳一個以JSON 形式表示的string 或在失敗時傳回FALSE 。 

Report a bug 更新日誌 
版本 說明 
5.4.0 options 參數增加常數: JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, 和 JSON_UNESCAPED_UNICODE。 
5.3.3 options 參數增加常數:JSON_NUMERIC_CHECK。
5.3.0 增加options 參數. 


Report a bug 範例 

Example #1 A json_encode() 的範例 

#複製程式碼如下:

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=> 4,'e'=>5); 


echo json_encode($arr); 
?> 


以上例程會輸出: 

#複製程式碼如下:

{"a":1,"b":2,"c":3,"d":4,"e":5} 


Example #2 json_encode() 函數中options 參數的用法 

複製程式碼如下:

##$a = array(' ',"'bar'",'"baz"','&blong&', "\xc3\xa9"); 

echo "Normal: ", json_encode($a), "\ n"; 
echo "Tags: ", json_encode($a, JSON_HEX_TAG), "\n"; 
echo "Apos: ", json_encode($a, JSON_HEX_APOS), "\n"; 
echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "\n"; 
echo "Amp: ", json_encode($a, JSON_HEX_AMP), "\n"; 
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n"; 
echo "All: ", json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMPAMP JSON_U),CAPOS | #$b = array(); 

echo "Empty array output as array: ", json_encode($b), "\n"; 
echo "Empty array output as object: ", json_encode( $b, JSON_FORCE_OBJECT), "\n\n"; 

$c = array(array(1,2,3)); 

echo "Non-associative array output as array: ", json_encode($c), "\n"; 
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n"; 

##> d = array('foo' => 'bar', 'baz' => 'long'); 

echo "Associative array always output as object: ", json_encode($d), "\ n"; 
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n"; 
?> 


以上例程會輸出: 

複製程式碼如下:


Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"] 
Tags: ["\u003Cfoo\u003E ","'bar'","\"baz\"","&blong&","\u00e9"] 
Apos: ["","\u0027bar\u0027","\"baz\ "","&blong&","\u00e9"] 
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"] 
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"] 
Unicode: [""," 'bar'","\"baz\"","&blong&","é"] 
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\ u0026blong\u0026","é"] 

Empty array output as array: [] 
Empty array output as object: {} 

Non-associative array output as 1 1,2,3]] 
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}} 

#Associative array always output as object: {"foo":"bar","baz":"long"} 
Associative array always output as object: {"foo":"bar","baz":"long"}


Example #3 連續與非連續陣列範例 

複製程式碼如下:


echo "連續陣列" .PHP_EOL; 
$sequential = array("foo", "bar", "baz", "blong"); 
var_dump( 
$sequential, 
json_encode($sequential) 
$sequential, 
json_encode($sequential) 
#$sequential, 
json_encode($sequential) 
#$sequential, 
json_encode($sequential) 
#$ ); 

echo PHP_EOL."非連續陣列".PHP_EOL; 
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz" , 4=>"blong"); 
var_dump( 
$nonsequential, 
json_encode($nonsequential) 
#); 

#echo PHP_EOL."刪除一個連續陣列值的方式產生的非連續數組".PHP_EOL; 
unset($sequential[1]); 

var_dump( 
$sequential, 
json_encode($sequential) 

#); 

? > 



以上程式會輸出: 

複製程式碼如下:


連續陣列 
#array(4) { 
[0]=> 
string(3) "foo" 
[1]=> 
string(3) "bar" 
[2]=> 
string(3 ) "baz" 
[3]=> 
string(5) "blong" 

string(27) "["foo","bar","baz","blong "]" 

非連續陣列 
array(4) { 
#[1]=> 
string(3) "foo" 
#[2]=> 
string(3) "bar" 
[3]=> 
string(3) "baz" 
[4]=> 
string(5) "blong" 
#}
string(43) "{"1":"foo","2":"bar","3":"baz","4":"blong"}" 

刪除一個連續數組值的方式產生的非連續數組 
array(3) { 
[0]=> 
string(3) "foo" 
[2]=> 

string (3) "baz" 
[3]=> 

string(5) "blong" 

string(33) "{"0":"foo","2": "baz","3":"blong"}" 



複製程式碼如下:



$obj->Name= 'a1'; $obj->Number ='123'; 

$obj->Contno= '000'; 

echo json_encode($obj); 



結果為: 

#複製程式碼如下:


{"Name":"a1", ###"Number":"123", ###"Contno":"000" ### } #########可以看出json_encode()和json_decode()是編譯和反編譯過程,注意json只接受utf-8編碼的字符,所以json_encode()的參數必須是utf-8編碼,否則會得到空字元或null。

以上是對比json_encode和json_decode的區別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn