4

我是 Mongo 的新手,正在尝试从集合中导出 JSON 文件。> MONGOEXPORT运行良好并创建了一个 JSON 文件。

{ "_id" : { "$oid" : "54c8f3fb5e24e03c473243c4" }, "username" : "Aman", "password" : "yesboss" }
{ "_id" : { "$oid" : "54c901c1953b434dabadbabf" }, "username" : "AMAN2" }

现在,JSONLint 向我显示它不是有效的 JSON 的错误。

我需要将其导入我的 Java 项目以从中提取值。

4

1 回答 1

6

MongoExport 导出有效的 JSON。您的 Java 应该将每一行而不是整个文件解析为 JSON 对象。

如果要将整个导出文件视为 JSON 对象,请使用该选项--jsonArray

--jsonArray
output to a json array rather than one 
object per line

例子:

mongoexport --db test -c x
connected to: 127.0.0.1
{ "_id" : "54c8f3fb5e24e03c473243c4", "username" : "Aman", "password" : "yesboss" }
{ "_id" : "54c901c1953b434dabadbabf", "username" : "AMAN2" }
exported 2 records

mongoexport --db test -c x --jsonArray
connected to: 127.0.0.1
[{ "_id" : "54c8f3fb5e24e03c473243c4", "username" : "Aman", "password" : "yesboss" },{ "_id" : "54c901c1953b434dabadbabf", "username" : "AMAN2" }]
exported 2 records
于 2015-01-29T04:53:35.027 回答