Pra*_*eep 4 .net c# json mongodb mongodb-.net-driver
我的 Json 字符串为
string myjson = "[
{
"col1": "1",
"col2": "2",
"col3": "3"
},
{
"col1": "4",
"col2": "5",
"col3": "6"
},
{
"col1": "7",
"col2": "8",
"col3": "9"
}]";
Run Code Online (Sandbox Code Playgroud)
问题是:当我创建 bson 文档时,它显示
无法将 BsonArray 转换为 BsonDocument
这就是我创建 BsonDocument 的方式:
BsonDocument doc = BsonSerializer.Deserialize<BsonDocument>(myjson);
Run Code Online (Sandbox Code Playgroud)
我应该怎么办?
BsonDocument doc = new BsonDocument();
BsonArray array = BsonSerializer.Deserialize<BsonArray>(myjson);
doc.Add(array);
Run Code Online (Sandbox Code Playgroud)
我没有尝试过,但它应该有效。
编辑:
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));
Run Code Online (Sandbox Code Playgroud)
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) }
};
Run Code Online (Sandbox Code Playgroud)
我能做的最好的就是这个。
| 归档时间: |
|
| 查看次数: |
8578 次 |
| 最近记录: |