函式名稱:MongoDB\BSON\Document::fromJSON()
函式描述:將JSON字串轉換為MongoDB\BSON\Document物件。
用法:
public static function fromJSON(string $json, array $options = []): MongoDB\BSON\Document|bool
引數:
$json
:要轉換的JSON字串。$options
(可選):附加選項陣列,用於配置轉換行為。
返回值:
- 如果解析成功,則返回一個MongoDB\BSON\Document物件。
- 如果解析失敗,則返回false。
示例:
$json = '{"name": "John Doe", "age": 25, "email": "[email protected]"}';
// 將JSON字串轉換為MongoDB\BSON\Document物件
$document = MongoDB\BSON\Document::fromJSON($json);
// 列印MongoDB\BSON\Document物件
var_dump($document);
// 輸出結果:
// object(MongoDB\BSON\Document)#1 (3) {
// ["storage":"ArrayObject":private]=>
// array(3) {
// ["name"]=>
// string(8) "John Doe"
// ["age"]=>
// int(25)
// ["email"]=>
// string(19) "[email protected]"
// }
// ["dataType":"ArrayObject":private]=>
// int(1)
// ["flags":"ArrayObject":private]=>
// int(2)
// }
注意事項:
$json
引數必須是一個有效的JSON字串,否則解析將失敗。$options
引數可用於配置解析行為,如指定解析選項或處理日期和時間的方式等。