JsonWriterSettings jsonWriterSettings = new JsonWriterSettings();
jsonWriterSettings.OutputMode = JsonOutputMode.Strict;
var a = docs.ToJson(jsonWriterSettings);
对于objectId类型和时间类型转化出来是这样
[{ "_id" : { "$oid" : "5da97496532e46bef095b9fe" }, "time" : { "$date" : 1571386506000 }, "picture_1" : "123", "picture_2" : "123", "picture_3" : "123", "picture_4" : "123", "goodsintro" : "123", "deletestate" : 0, "state" : "上架", "realsold" : 0, "inventory" : "0", "totalsold" : 0 }]
var a = docs.ToJson();
则是这种
{"_id" : ObjectId("5001047632e3988ad237a206"), "name" : "xx", "date" : ISODate("2012-07-14T05:32:38.189Z")}
使用的驱动是 MongoDB.Driver;MongoDB.Bson;MongoDB.Bson.IO;
那么对于ObjectId 和 ISODate 时间的问题我该如何序列化成正常的json呢?
反序列化出来也需要用Mongo自带的反序列化功能,例如:
var jsonlist = BsonSerializer.Deserialize<List<ChannelUserDaily>>(res.ToJson());
需要使用:
BsonSerializer.Deserialize
ChannelUserDaily实体中,属性添加BsonElement
比如:
/// <summary>
/// 房间号
/// </summary>
[BsonElement("av_id")]
public long AVID { get; set; }
/// <summary>
/// appid
/// </summary>
[BsonElement("appId")]
public string AppId { get; set; }
/// <summary>
/// 插入数据库时间
/// </summary>
[BsonElement("_timestamp")]
public long TimesTamp { get; set; }
谢谢你的回答
根据实际情况已经采用了另外一种方式
BsonTypeMapper.MapToDotNetValue