久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

PHP擴展之JavaScript對象符號(JSON)

瀏覽:138日期:2022-09-15 17:38:34

自 PHP 5.2.0 起,JSON 擴展默認內置并編譯進了 PHP。

JSON 序列化接口JsonSerializable

實現?JsonSerializable?的類可以 在?json_encode()?時定制他們的 JSON 表示法。

JsonSerializable::jsonSerialize?—?指定需要被序列化成 JSON 的數據

Example #1 ?返回一個數組

<?php class ArrayValue implements JsonSerializable {public function __construct(array $array) { $this->array = $array;}public function jsonSerialize() { return $this->array;} } $array = [1, 2, 3]; echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);?>

以上例程會輸出:

[ 1, 2, 3]

Example #2 ?返回了一個關聯數組

<?php class ArrayValue implements JsonSerializable {public function __construct(array $array) { $this->array = $array;}public function jsonSerialize() { return $this->array;} } $array = [’foo’ => ’bar’, ’quux’ => ’baz’]; echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);?>

以上例程會輸出:

{ 'foo': 'bar', 'quux': 'baz'}

Example #3 ?返回一個整型數字

<?php class IntegerValue implements JsonSerializable {public function __construct($number) { $this->number = (integer) $number;}public function jsonSerialize() { return $this->number;} } echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);?>

以上例程會輸出:

1

Example #4 返回一個字符串

<?php class StringValue implements JsonSerializable {public function __construct($string) { $this->string = (string) $string;}public function jsonSerialize() { return $this->string;} } echo json_encode(new StringValue(’Hello!’), JSON_PRETTY_PRINT);?>

以上例程會輸出:

'Hello!'JSON 函數json_decode?— 對 JSON 格式的字符串進行編碼json_encode?— 對變量進行 JSON 編碼json_last_error_msg?— Returns the error string of the last json_encode() or json_decode() calljson_last_error?— 返回最后發生的錯誤

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 东丰县| 天镇县| 五大连池市| 广宗县| 理塘县| 陇南市| 扶风县| 鄂托克旗| 扶余县| 荃湾区| 灵山县| 富裕县| 库车县| 衡阳市| 二连浩特市| 延津县| 鄂托克前旗| 贡嘎县| 浮梁县| 纳雍县| 阜新市| 嘉荫县| 平果县| 武宣县| 阿克| 法库县| 集安市| 洪洞县| 凉城县| 平泉县| 明星| 文水县| 甘德县| 金平| 惠来县| 钦州市| 肃宁县| 盐亭县| 舟山市| 错那县| 池州市|