适用于Java的mongo驱动程序3.x中的JSON.parse()等效项 [英] JSON.parse() equivalent in mongo driver 3.x for Java

查看:117
本文介绍了适用于Java的mongo驱动程序3.x中的JSON.parse()等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON.parse()(Java驱动程序)返回BasicDBList或BasicDBObject.

JSON.parse() from mongo (Java driver) returns either a BasicDBList or a BasicDBObject.

但是,当迁移到mongo驱动程序3.x时,返回DocumentList<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并获取DocumentList<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆