JSON的四個基本準則
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));
# ?>
上例輸出:
["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] => 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。
$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 參數的用法
複製程式碼如下:
# php
$a = array('
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: ["
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\""," &blong&","\u00e9"]
Apos: ["
Quot
Quot ["
Amp: ["
Unicode: ["
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"}
複製程式碼如下:
連續陣列
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)
}
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 參數的用法
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: ["
Tags: ["\u003Cfoo\u003E ","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["
Quot: ["
Amp: ["
Unicode: ["
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"}
複製程式碼如下:
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]);
#);
? >
以上程式會輸出:
複製程式碼如下:
連續陣列
#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(5) "blong"
}string(33) "{"0":"foo","2": "baz","3":"blong"}"
複製程式碼如下:
$obj->Contno= '000';
echo json_encode($obj);
結果為:
#複製程式碼如下:
以上是對比json_encode和json_decode的區別的詳細內容。更多資訊請關注PHP中文網其他相關文章!