【发布时间】:2019-12-06 09:51:12
【问题描述】:
我有一个 JSON 格式的字符串,我想将其转换为 BSONDocument 以插入到 LiteDB 数据库中。我该如何进行转换?我正在使用 LiteDB 5.0.0-beta (我也在 LiteDB v4.1.4 中对其进行了测试)。这是代码;
MyHolder holder = new MyHolder
{
Json = "{\"title\":\"Hello World\"}"
};
BsonDocument bsonDocument = BsonMapper.Global.ToDocument(holder.Json);
// bsonDocument returns null in v5, and throws exception in v4.1.4
mongoDB中的另一个例子,你可以这样做(Convert string into MongoDB BsonDocument);
string json = "{ 'foo' : 'bar' }";
MongoDB.Bson.BsonDocument document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(json);
到目前为止我也尝试过;
string json = "{ 'foo' : 'bar' }";
byte[] bytes = Encoding.UTF8.GetBytes(json);
BsonDocument bsonDocument = LiteDB.BsonSerializer.Deserialize(bytes); // throws "BSON type not supported".
也试过了;
BsonDocument bsonDocument = BsonMapper.Global.ToDocument(json); // Returns null bsonDocument.
【问题讨论】: