一、json_encode()
[php] view plain copy
<?php
$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}
再看一個物件轉換的例子:
$obj->body = "another post";
$obj->id = 21;
$obj->approved = true;
$obj->favorite_count = 1;
$obj->status = NULL;
echo json_encode($obj);
{
"body":"another post",
"id":21,
"approved":true,
"favorite_count":1,
"status":null
}
二、索引陣列和關聯陣列
PHP支援兩種陣列,一種是隻儲存"值"(value)的索引陣列(indexed array),另一種是儲存"名值對"(name/value)的關聯陣列(associative array)。
由於javascript不支援關聯陣列,所以json_encode()只將索引陣列(indexed array)轉為陣列格式,而將關聯陣列(associative array)轉為物件格式。
比如,現在有一個索引陣列
$arr = Array("one","two", "three");
["one","two","three"]
如果將它改為關聯陣列:
$arr = Array("1"=>"one","2"=>"two","3"=>"three");
輸出變為
{"1":"one","2":"two","3":"three"}
如果你需要將"索引陣列"強制轉化成"物件",可以這樣寫
json_encode( (object)$arr);
或者
json_encode ( $arr, JSON_FORCE_OBJECT );
下面是一個PHP的類:
class Foo {
const ERROR_CODE = "404";
public $public_ex ="this is public";
private $private_ex ="this is private!";
protected$protected_ex ="this should be protected";
publicfunction getErrorCode() {
returnself::ERROR_CODE;
$foo =new Foo;
$foo_json = json_encode($foo);
echo $foo_json;
{"public_ex":"this is public"}
該函式用於將json文字轉換為相應的PHP資料結構。下面是一個例子:
$json ="{"foo": 12345}";
$obj = json_decode($json);
print $obj->{"foo"};// 12345
通常情況下,json_decode()總是返回一個PHP物件,而不是陣列。比如:
$json ="{"a":1,"b":2,"c":3,"d":4,"e":5}";
var_dump(json_decode($json));
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
var_dump(json_decode($json,true));
結果就生成了一個關聯陣列:
array(5) {
下面三種json寫法都是錯的,你能看出錯在哪裡嗎?五、json_decode()的常見錯誤
$bad_json ="{ "bar": "baz" }";
$bad_json ="{ bar: "baz" }";
$bad_json ="{ "bar": "baz", }";
另外,json只能用來表示物件(object)和陣列(array),如果對一個字串或數值使用json_decode(),將會返回null。
var_dump(json_decode("Hello World"));//null
一、json_encode()
[php] view plain copy
<?php
$arr =array ("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);
echo json_encode($arr);
?>
輸出
[php] view plain copy
{"a":1,"b":2,"c":3,"d":4,"e":5}
再看一個物件轉換的例子:
[php] view plain copy
$obj->body = "another post";
$obj->id = 21;
$obj->approved = true;
$obj->favorite_count = 1;
$obj->status = NULL;
echo json_encode($obj);
輸出
[php] view plain copy
{
"body":"another post",
"id":21,
"approved":true,
"favorite_count":1,
"status":null
}
由於json只接受utf-8編碼的字元,所以json_encode()的引數必須是utf-8編碼,否則會得到空字元或者null。當中文使用GB2312編碼,或者外文使用ISO-8859-1編碼的時候,這一點要特別注意。二、索引陣列和關聯陣列
PHP支援兩種陣列,一種是隻儲存"值"(value)的索引陣列(indexed array),另一種是儲存"名值對"(name/value)的關聯陣列(associative array)。
由於javascript不支援關聯陣列,所以json_encode()只將索引陣列(indexed array)轉為陣列格式,而將關聯陣列(associative array)轉為物件格式。
比如,現在有一個索引陣列
[php] view plain copy
$arr = Array("one","two", "three");
echo json_encode($arr);
輸出
[php] view plain copy
["one","two","three"]
如果將它改為關聯陣列:
[php] view plain copy
$arr = Array("1"=>"one","2"=>"two","3"=>"three");
echo json_encode($arr);
輸出變為
[php] view plain copy
{"1":"one","2":"two","3":"three"}
注意,資料格式從"[]"(陣列)變成了"{}"(物件)。如果你需要將"索引陣列"強制轉化成"物件",可以這樣寫
[php] view plain copy
json_encode( (object)$arr);
或者
[php] view plain copy
json_encode ( $arr, JSON_FORCE_OBJECT );
三、類(class)的轉換下面是一個PHP的類:
[php] view plain copy
class Foo {
const ERROR_CODE = "404";
public $public_ex ="this is public";
private $private_ex ="this is private!";
protected$protected_ex ="this should be protected";
publicfunction getErrorCode() {
returnself::ERROR_CODE;
}
}
現在,對這個類的例項進行json轉換:[php] view plain copy
$foo =new Foo;
$foo_json = json_encode($foo);
echo $foo_json;
輸出結果是[php] view plain copy
{"public_ex":"this is public"}
四、json_decode() 可以看到,除了公開變數(public),其他東西(常量、私有變數、方法等等)都遺失了。該函式用於將json文字轉換為相應的PHP資料結構。下面是一個例子:
[php] view plain copy
$json ="{"foo": 12345}";
$obj = json_decode($json);
print $obj->{"foo"};// 12345
通常情況下,json_decode()總是返回一個PHP物件,而不是陣列。比如:
[php] view plain copy
$json ="{"a":1,"b":2,"c":3,"d":4,"e":5}";
var_dump(json_decode($json));
結果就是生成一個PHP物件:[php] view plain copy
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
如果想要強制生成PHP關聯陣列,json_decode()需要加一個引數true:[php] view plain copy
$json ="{"a":1,"b":2,"c":3,"d":4,"e":5}";
var_dump(json_decode($json,true));
結果就生成了一個關聯陣列:
[php] view plain copy
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
下面三種json寫法都是錯的,你能看出錯在哪裡嗎?五、json_decode()的常見錯誤
[php] view plain copy
$bad_json ="{ "bar": "baz" }";
$bad_json ="{ bar: "baz" }";
$bad_json ="{ "bar": "baz", }";
第一個的錯誤是,json的分隔符(delimiter)只允許使用雙引號,不能使用單引號。第二個的錯誤是,json名值對的"名"(冒號左邊的部分),任何情況下都必須使用雙引號。第三個的錯誤是,最後一個值之後不能新增逗號(trailing comma)。 對這三個字串執行json_decode()都將返回null,並且報錯。另外,json只能用來表示物件(object)和陣列(array),如果對一個字串或數值使用json_decode(),將會返回null。
[php] view plain copy
var_dump(json_decode("Hello World"));//null