Java 从 JSON 字符串创建 BSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3117167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:36:18  来源:igfitidea点击:

Creating BSON object from JSON string

javajsonmongodbbson

提问by Maciek Sawicki

I have Java app that takes data from external app. Incoming JSONs are in Strings. I would like to parse that Strings and create BSON objects.

我有从外部应用程序获取数据的 Java 应用程序。传入的 JSON 是字符串。我想解析该字符串并创建 BSON 对象。

Unfortunate I can't find API for that in Java's BSON implementation.

不幸的是,我在 Java 的 BSON 实现中找不到相应的 API。

Do I have use external parser for that like GSON?

我有像 GSON 这样的外部解析器吗?

采纳答案by Hank Gay

The easiest way seems to be to use a JSON library to parse the JSON strings into a Mapand then use the putAllmethod to put those values into a BSONObject.

最简单的方法似乎是使用 JSON 库将 JSON 字符串解析为 a Map,然后使用该putAll方法将这些值放入BSONObject.

This answershows how to use Hymansonto parse a JSON string into a Map.

此答案显示了如何使用Hymanson将 JSON 字符串解析为Map.

回答by StaxMan

You might be interested in bson4Hymansonproject, which allows you to use Hymanson data binding to work with BSON (create POJOs from BSON, write as BSON) -- especially since Hymanson also work with JSON. So it will allow conversion like you mention, just use different ObjectMapper instanstaces (one that works with JSON, other with BSON).

您可能对bson4Hymanson项目感兴趣,项目允许您使用 Hymanson 数据绑定来处理 BSON(从 BSON 创建 POJO,编写为 BSON)——尤其是因为 Hymanson 也使用 JSON。因此,它将允许像您提到的那样进行转换,只需使用不同的 ObjectMapper 实例(一个适用于 JSON,另一个适用于 BSON)。

With Hymanson you can either work with full POJOs (declare structure you want) or with simple Maps, Lists and so on. You just need to declare what to type to bind to when reading data (when writing, type is defined by object you pass).

使用 Hymanson,您可以使用完整的 POJO(声明您想要的结构)或使用简单的 Maps、Lists 等。您只需要在读取数据时声明要绑定的类型(写入时,类型由您传递的对象定义)。

回答by mayank_gupta

I am not sure about java but the mongoDB CPP driver has a function type

我不确定 java,但 mongoDB CPP 驱动程序有一个函数类型

BSONObj fromjson(string)

BSONObj fromjson(string)

which returns a BSONObj according to the string passed. There should be a same function in Java too.

根据传递的字符串返回 BSONObj 。Java 中也应该有相同的功能。

回答by Kresten Krab Thorup

You'll find the answer to your question in the source code of https://github.com/mongodb/mongo/blob/master/src/mongo/db/jsobj.cppWhich has the BSON to JSON conversion.

您可以在https://github.com/mongodb/mongo/blob/master/src/mongo/db/jsobj.cpp的源代码中找到问题的答案, 其中包含 BSON 到 JSON 的转换。

Basically, stuff like

基本上,像

  • ObjectId("XXX")-> { "$oid" : "XXX" }
  • /XXX/gi-> { "$regex" : "XXX", "$options" : "gi" }
  • ObjectId("XXX")-> { "$oid" : "XXX" }
  • /XXX/gi-> { "$regex" : "XXX", "$options" : "gi" }

and so on...

等等...

回答by eskatos

Official MongoDB Java Driver comes with utility methods for parsing JSON to BSON and serializing BSON to JSON.

官方 MongoDB Java 驱动程序带有用于将 JSON 解析为 BSON 并将 BSON 序列化为 JSON 的实用方法。

import com.mongodb.DBObject;
import com.mongodb.util.JSON;

DBObject dbObj = ... ;
String json = JSON.serialize( dbObj );
DBObject bson = ( DBObject ) JSON.parse( json );

The driver can be found here: https://mongodb.github.io/mongo-java-driver/

驱动程序可以在这里找到:https: //mongodb.github.io/mongo-java-driver/

回答by Leticia Santos

To convert a string json to bson, do:

要将字符串 json 转换为 bson,请执行以下操作:

import org.bson.BasicBSONEncoder;
import org.bson.BSONObject;

BSONObject bson = (BSONObject)com.mongodb.util.JSON.parse(string_json);
BasicBSONEncoder encoder = new BasicBSONEncoder();
byte[] bson_byte = encoder.encode(bson);

To convert a bson to json, do:

要将 bson 转换为 json,请执行以下操作:

import org.bson.BasicBSONDecoder;
import org.bson.BSONObject;

BasicBSONDecoder decoder = new BasicBSONDecoder();
BSONObject bsonObject = decoder.readObject(out);
String json_string = bsonObject.toString();

回答by yair

... And, since 3.0.0, you can:

...而且,从 3.0.0 开始,您可以:

import org.bson.Document;

final Document doc = new Document("myKey", "myValue");
final String jsonString = doc.toJson();
final Document doc = Document.parse(jsonString);

Official docs:

官方文档:

回答by user2023448

I would suggest using the toJson() and parse(String) methods of the BasicDBObject, because the JSON utility class has been @Depricated.

我建议使用 BasicDBObject 的 toJson() 和 parse(String) 方法,因为 JSON 实用程序类已被@Depricated。

import com.mongodb.BasicDBObject;

public static BasicDBObject makeBsonObject(String json) {
    return BasicDBObject.parse(json);
}

public static String makeJsonObject(BasicDBObject dbObj) {
    return dbObj.toJson();
}

回答by ultimatex

Use Document.parse(String json)from org.bson.Document. It returns Documentobject which is type of Bson.

Document.parse(String json)从使用org.bson.Document。它返回Document类型为 的对象Bson