函数名称: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": "johndoe@example.com"}';
// 将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) "johndoe@example.com"
// }
// ["dataType":"ArrayObject":private]=>
// int(1)
// ["flags":"ArrayObject":private]=>
// int(2)
// }
注意事项:
$json
参数必须是一个有效的JSON字符串,否则解析将失败。$options
参数可用于配置解析行为,如指定解析选项或处理日期和时间的方式等。