Deserialize a Collection from BSON (newtonsoft.com)
This sample sets ReadRootValueAsArray to true so the root BSON value is correctly read as an array instead of an object and deserializes BSON to a collection.
![](https://www.newtonsoft.com/json/help/icons/SectionExpanded.png)
Types
public class Event
{
public string Name { get; set; }
public DateTime StartDate { get; set; }
}
Usage
string s = "MQAAAAMwACkAAAACTmFtZQAHAAAARWFzdGVyAAlTdGFydERhdGUAgDf0uj0BAAAAAA==";
byte[] data = Convert.FromBase64String(s);
MemoryStream ms = new MemoryStream(data);
using (BsonReader reader = new BsonReader(ms))
{
reader.ReadRootValueAsArray = true;
JsonSerializer serializer = new JsonSerializer();
IList<Event> events = serializer.Deserialize<IList<Event>>(reader);
Console.WriteLine(events.Count);
// 1
Console.WriteLine(events[0].Name);
// Easter
}
转自:http://Www.CnBlogs.Com/WebEnh/
如果想下次快速找到我,记得点下面的关注哦!
本博客Android APP 下载 |
![]() |
支持我们就给我们点打赏 |
![]() |
支付宝打赏 支付宝扫一扫二维码 |
![]() |
微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!