溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP操作json的案例分析

發布時間:2020-07-18 15:02:58 來源:億速云 閱讀:126 作者:清晨 欄目:編程語言

這篇文章主要介紹PHP操作json的案例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、json_encode()

該函數主要用來將數組和對象,轉換為json格式。

先看一個數組轉換的例子:

$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
  }

由于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)轉為對象格式。

比如,現在有一個索引數組

$arr = Array('one', 'two', 'three');
echo json_encode($arr);

結果為:

["one","two","three"]

如果將它改為關聯數組:

$arr = Array('1'=>'one', '2'=>'two', '3'=>'three');
echo json_encode($arr);

結果就變了:

 {"1":"one","2":"two","3":"three"}

注意,數據格式從"[]"(數組)變成了"{}"(對象)。

如果你需要將"索引數組"強制轉化成"對象",可以這樣寫

json_encode( (object)$arr );

或者

json_encode ( $arr, JSON_FORCE_OBJECT );

三、類(class)的轉換

下面是一個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';

public function getErrorCode() {

return self::ERROR_CODE;

}

}

現在,對這個類的實例進行json轉換:

$foo = new Foo;

$foo_json = json_encode($foo);

echo $foo_json;

輸出結果是

{"public_ex":"this is public"}

可以看到,除了公開變量(public),其他東西(常量、私有變量、方法等等)都遺失了。

四、json_decode()

該函數用于將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));

結果就是生成一個PHP對象:

object(stdClass)#1 (5) {

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

}

如果想要強制生成PHP關聯數組,json_decode()需要加一個參數true:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json,true));

結果就生成了一個關聯數組:

array(5) {

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

}

五、json_decode()的常見錯誤

下面三種json寫法都是錯的,你能看出錯在哪里嗎?

$bad_json = "{ 'bar': 'baz' }";

$bad_json = '{ bar: "baz" }';

$bad_json = '{ "bar": "baz", }';

對這三個字符串執行json_decode()都將返回null,并且報錯。

第一個的錯誤是,json的分隔符(delimiter)只允許使用雙引號,不能使用單引號。第二個的錯誤是,json名值對的"名"(冒號左邊的部分),任何情況下都必須使用雙引號。第三個的錯誤是,最后一個值之后不能添加逗號(trailing comma)。

另外,json只能用來表示對象(object)和數組(array),如果對一個字符串或數值使用json_decode(),將會返回null。

var_dump(json_decode("Hello World")); //null

以上是PHP操作json的案例分析的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女