将BSON转换为有效的JSON

13
< p > BsonDocument.ToJson() 方法返回的 JSON 无效,因为 ObjectID()ISODate 不是有效的 JSON。

从任意 BSON 文档获取有效的 JSON 的最佳方法是什么?


2
我觉得很奇怪的是"strict" mode不是默认设置,但事实确实如此。猜测可能更多的用户投票支持能够直接将输出“剪切/粘贴”到MongoDB shell中。 - Blakes Seven
1个回答

14

你可以尝试类似这样的东西

var document = new BsonDocument("_id", ObjectId.GenerateNewId());
    var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; // key part
    Console.WriteLine(document.ToJson(jsonWriterSettings));

更多信息请访问https://groups.google.com/forum/#!topic/mongodb-user/fQc9EvsPc4k


1
我已经做了这个,但是当我在API中返回这个JSON时,我得到了所有尾随的\符号。如何处理它? - Vulovic Vukasin
5
现在,我得到的不再是"Property" : NumberDecimal("23.44"), 而是"Property" : { "$numberDecimal": "23.44"} 。这能不能像普通人一样直接将小数值放入结果中呢?我只想要"Property" : 23.44 - Piotr Kula

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接