Bson - 如何将 JSON 转换为 List<Document> 并将 List<Document> 转换为 JSON?
Bson - How to convert JSON to List<Document> and List<Document> to JSON?
我正在使用 Java Driver 3.0 和 MongoDB 以便通过网络服务发送 JSONs。
当我想将文档对象 (org.bson.Document) 转换为 JSON 时,我使用 obj.toJson()
,当我想将 JSON 转换为文档时对象,我使用 Document.parse(json)
.
然而,当我处理文档列表时(在 JSON:[{"field1":1, ...}, {"field1":2, ...}]
中表示),我想不出一种干净的方法来进行这些转换。
到目前为止,我已经想出了这些 "hacks":
从列表到 JSON:我将文档列表添加为更大文档中名为 "list" 的字段的值。我将这个大文档转换为JSON,并从获取的String中删除我不需要的内容。
public String toJson(List<Document> docs){
Document doc = new Document("list", docs);
String json = doc.toJson();
return json.substring(json.indexOf(":")+2, json.length()-1);
}
从 JSON 到列表:我通过将此 "list" 字段添加到 JSON,将其转换为文档并仅获取值来做相反的事情该字段来自文档。
public static List<Document> toListOfDocuments(String json){
Document doc = Document.parse("{ \"list\":"+json+"}");
Object list = doc.get("list");
if(list instanceof List<?>) {
return (List<Document>) doc.get("list");
}
return null ;
}
我还尝试使用另一个 JSON 序列化程序(我使用了 Google 的序列化程序),但它没有给出与内置 toJson()
方法相同的结果来自 Document 对象,特别是“_id”字段或时间戳。
有什么干净的方法可以做到这一点吗?
com.mongodb.util.JSON
包 "still" 未被弃用,并且可以很好地处理 DBObject
的列表。你只需要做一点转换:
MongoClient client = new MongoClient(new ServerAddress("192.168.2.4", 27017));
MongoDatabase db = client.getDatabase("test");
MongoCollection<Document> collection = db.getCollection("sample");
MongoCursor<Document> iterator = collection.find().iterator();
BasicDBList list = new BasicDBList();
while (iterator.hasNext()) {
Document doc = iterator.next();
list.add(doc);
}
System.out.println(JSON.serialize(list));
并且将 "list" 添加到另一个 DBObject
并使用输出中使用的密钥 "list" 并没有错。否则,您可以深入研究使用另一个 JSON 解析器并将游标迭代器中的每个文档提供给它。
这取决于您输入的大小,但虽然这仍然有效,但它确实在代码中看起来更清晰。
有驱动3.0的解决方案。
您按照以下步骤操作:
BasicDBObject dbObject = (BasicDBObject) JSON.parse("yourJsonString");
MongoCollection<BasicDBObject> table = db.getCollection("collectionName", BasicDBObject.class);
table.insertOne(dbObject);
我正在使用 Java Driver 3.0 和 MongoDB 以便通过网络服务发送 JSONs。
当我想将文档对象 (org.bson.Document) 转换为 JSON 时,我使用 obj.toJson()
,当我想将 JSON 转换为文档时对象,我使用 Document.parse(json)
.
然而,当我处理文档列表时(在 JSON:[{"field1":1, ...}, {"field1":2, ...}]
中表示),我想不出一种干净的方法来进行这些转换。
到目前为止,我已经想出了这些 "hacks":
从列表到 JSON:我将文档列表添加为更大文档中名为 "list" 的字段的值。我将这个大文档转换为JSON,并从获取的String中删除我不需要的内容。
public String toJson(List<Document> docs){ Document doc = new Document("list", docs); String json = doc.toJson(); return json.substring(json.indexOf(":")+2, json.length()-1); }
从 JSON 到列表:我通过将此 "list" 字段添加到 JSON,将其转换为文档并仅获取值来做相反的事情该字段来自文档。
public static List<Document> toListOfDocuments(String json){ Document doc = Document.parse("{ \"list\":"+json+"}"); Object list = doc.get("list"); if(list instanceof List<?>) { return (List<Document>) doc.get("list"); } return null ; }
我还尝试使用另一个 JSON 序列化程序(我使用了 Google 的序列化程序),但它没有给出与内置 toJson()
方法相同的结果来自 Document 对象,特别是“_id”字段或时间戳。
有什么干净的方法可以做到这一点吗?
com.mongodb.util.JSON
包 "still" 未被弃用,并且可以很好地处理 DBObject
的列表。你只需要做一点转换:
MongoClient client = new MongoClient(new ServerAddress("192.168.2.4", 27017));
MongoDatabase db = client.getDatabase("test");
MongoCollection<Document> collection = db.getCollection("sample");
MongoCursor<Document> iterator = collection.find().iterator();
BasicDBList list = new BasicDBList();
while (iterator.hasNext()) {
Document doc = iterator.next();
list.add(doc);
}
System.out.println(JSON.serialize(list));
并且将 "list" 添加到另一个 DBObject
并使用输出中使用的密钥 "list" 并没有错。否则,您可以深入研究使用另一个 JSON 解析器并将游标迭代器中的每个文档提供给它。
这取决于您输入的大小,但虽然这仍然有效,但它确实在代码中看起来更清晰。
有驱动3.0的解决方案。
您按照以下步骤操作:
BasicDBObject dbObject = (BasicDBObject) JSON.parse("yourJsonString");
MongoCollection<BasicDBObject> table = db.getCollection("collectionName", BasicDBObject.class);
table.insertOne(dbObject);