如何使用PHP将Mongo DB对象转换为JSON?

如何使用PHP将Mongo DB对象转换为JSON?,php,json,mongodb,Php,Json,Mongodb,我想用PHP将MongoDB对象转换为JSON。MongoDB提供的密钥名没有字符串,不支持JSON。我非常熟悉JSON,不知道如何在PHP中使用MongoDB对象。以下是我的源代码: <?php $str = '[{ DataFound: "DataFound"}]'; $str1 = json_decode($str,TRUE); var_dump($str1); ?> 它返回空值 既然我写了这篇文章,我意识到它并没有准确地回答你的问题,但它向你展示了如何调试js

我想用PHP将MongoDB对象转换为JSON。MongoDB提供的密钥名没有字符串,不支持JSON。我非常熟悉JSON,不知道如何在PHP中使用MongoDB对象。以下是我的源代码:

 <?php
 $str = '[{ DataFound: "DataFound"}]';
 $str1 = json_decode($str,TRUE);
 var_dump($str1);
 ?>


它返回空值

既然我写了这篇文章,我意识到它并没有准确地回答你的问题,但它向你展示了如何调试json,这可能有助于回答这个问题

调试json

<?php

$str = '[{DataFound: "DataFound"}]';
$str1 = json_decode($str,TRUE);
$error = json_last_error();
if($error !== JSON_ERROR_NONE){
    var_dump($error); // show error if there is one
}
var_dump($str1); // else show your array
?>
进一步阅读

可能与所问的问题更相关

进行了一些挖掘,可以看到mongo有一个命令将数据转储到csv中


也许这是一个很好的选择,然后您可以使用将csv导入数据库。我发现它是导入csv文件的最佳接口。

[{DataFound:“DataFound”}]
这不是一个有效的
JSON
我知道@sahil,如何使它成为有效的JSON?这是我的问题。关键应该用引号括起来。我应该是这样的
$str='[{“DataFound”:“DataFound”}]
$str = '[{"DataFound" : "DataFound"}]';
mongoexport --db users --collection contacts --type=csv --fields name,address --out /opt/backups/contacts.csv