从JSON字符串创建BSON对象

问题描述:

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

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的BSON实现中找不到API。

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

我是否像GSON那样使用外部解析器?

Do I have use external parser for that like GSON?

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

The easiest way seems to be to use a JSON library to parse the JSON strings into a Map and then use the putAll method to put those values into a BSONObject.

此答案显示了如何使用 Jackson 将JSON字符串解析为地图

This answer shows how to use Jackson to parse a JSON string into a Map.