适用于Java的mongo驱动程序3.x中的JSON.parse()等效项 [英] JSON.parse() equivalent in mongo driver 3.x for Java
问题描述
JSON.parse()
(Java驱动程序)返回BasicDBList或BasicDBObject.
JSON.parse()
from mongo (Java driver) returns either a BasicDBList or a BasicDBObject.
但是,当迁移到mongo驱动程序3.x时,返回Document
或List<Document>
的新解析方法是什么?
However, when migrating to mongo driver 3.x, what's the new parse method that returns either Document
or List<Document>
?
在新驱动程序中,Document.parse()
仅解析对象,而不解析数组(给定数组时引发异常).
In the new driver, Document.parse()
only parses an object, not an array (throws an exception when given an array).
带有3.x Java驱动程序的数组的JSON.parse()等价于什么?
What is the equivalent of JSON.parse() for Arrays with 3.x Java drivers ?
推荐答案
解析任何JSON并获取Document
或List<Document>
的简单技巧:
A simple trick to parse any JSON and to get either Document
or List<Document>
:
Document.parse("{\"json\":" + json + "}").get("json")
这篇关于适用于Java的mongo驱动程序3.x中的JSON.parse()等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!