无法在C#中将BsonArray转换为BsonDocument - c#

我有我的Json字符串作为

string myjson = "[
{
    "col1": "1",
    "col2": "2",
    "col3": "3"
},
{
    "col1": "4",
    "col2": "5",
    "col3": "6"
},
{
    "col1": "7",
    "col2": "8",
    "col3": "9"
}]";

问题是:当我创建bson文档时,它正在显示

无法将BsonArray转换为BsonDocument

这就是我创建BsonDocument的方式:

BsonDocument doc = BsonSerializer.Deserialize<BsonDocument>(myjson);

我该怎么办?

参考方案

BsonDocument doc = new BsonDocument();
BsonArray array = BsonSerializer.Deserialize<BsonArray>(myjson);
doc.Add(array);

我没有尝试过,但是应该可以。

编辑:

        string myjson1 = "{ 'col1': '1', 'col2': '2', 'col3': '3'}";
        string myjson2 = "{ 'col1': '4', 'col2': '5', 'col3': '6'}";
        string myjson3 = "{'col1': '7', 'col2': '8', 'col3': '9'}";

        BsonArray arr = new BsonArray();
        arr.Add(BsonSerializer.Deserialize<BsonDocument>(myjson1));
        arr.Add(BsonSerializer.Deserialize<BsonDocument>(myjson2));
        arr.Add(BsonSerializer.Deserialize<BsonDocument>(myjson3));

或者只是像这样在文档中包含一个values元素:

string myjson = "[ { 'col1': '1', 'col2': '2', 'col3': '3'},{ 'col1': '4', 'col2': '5', 'col3': '6'},{'col1': '7', 'col2': '8', 'col3': '9'}]";    
var doc = new BsonDocument {
                    { "values", BsonSerializer.Deserialize<BsonArray>(myjson) }
                };

我能做的最好的就是这个。

如何从php中获取datatables jQuery插件的json数据 - php

我是PHP的新手,正在尝试使用Datatables jQuery插件。我知道我必须从.php文件中获取数据,然后在数据表中使用它,但是我似乎无法通过ajax将其传递给数据表。我设法从数据库中获取数据,并对其进行json编码,但是后来我不知道如何在我的index.php文件中调用它,并在其中显示它。我究竟做错了什么?这是我的代码:HTML(已编辑): <…

如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…

MongoDB Fluent界面。尝试投影子文档属性,而忽略父文档属性 - c#

我有一个包含子文档的父文档(它不是数组,只是1:1关系)。{ _id: ObjectId(....) prop1 : value1 prop2 : value2 subdoc : { subProp1 : subPropValue1 subProp2 : subPropValue2 } } 我正在尝试使用Projection仅包括subProp1和subPr…

Mongo抛出“元素名称'名称'无效”异常 - c#

我正在更新一个简单的字段。var filterDocument = new BsonDocument { { "name", "alice" } }; var newDocument = new BsonDocument { { "name", "Alice" } }; coll…

T-SQL等价的正则表达式'\ b' - c#

我正在将利用regex的CLR函数转换为SQL函数。我知道SQL Server并不完全支持正则表达式,但是我只需要一种情况就可以搜索单词。搜索字段值:{"Id":1234, "Title": "The quick brown"}.NET中的正则表达式模式:'\b' + '…