Bson array (de)serialization with Json.NET

后端 未结 5 987
再見小時候
再見小時候 2020-12-07 01:30

I am simply trying to serialize and deserialize a string array in Bson format using Json.NET, but the following code fails:

var jsonSerializer = new JsonSeria         


        
5条回答
  •  渐次进展
    2020-12-07 02:23

    I know this is an old thread but I discovered a easy deserialization while using the power of MongoDB.Driver

    You can use BsonDocument.parse(JSONString) to deserialize a JSON object so to deserialize a string array use this:

    string Jsonarray = "[\"value1\", \"value2\", \"value3\"]";
    BsonArray deserializedArray = BsonDocument.parse("{\"arr\":" + Jsonarray + "}")["arr"].asBsonArray;
    

    deserializedArray can then be used as any array such as a foreach loop.

提交回复
热议问题