3

我目前正在尝试将 JSON 文件插入到我的 mongoDB 中。我已经看到过去通过使用 mongo::BSONObj 解决了这个问题......但这似乎不是一个选项,因为他们发布了用于 c++11 的新 mongocxx 驱动程序。这是我在 bsoncxx src 文件中找到的:

BSONCXX_API document::value BSONCXX_CALL from_json(stdx::string_view json);
/// Constructs a new document::value from the provided JSON text
///
/// @param 'json'
///  A string_view into a JSON document
///
/// @returns A document::value if conversion worked.
///
/// @throws bsoncxx::exception with error details if the conversion failed.
///

如何将我的 JSON 文件放入stdx::string_view?

谢谢!

4

1 回答 1

6

Absoncxx::stdx::string_view可以由 a 构造std::string。只需将您的文件内容(假设它包含一个 JSON 对象)加载到一个std::string(可能通过std::ifstream)中,然后将其传递std::stringbsoncxx::from_json. 从返回的对象bsoncxx::from_json是 a bsoncxx::document::value,它是包含 BSON 文档的资源拥有类型。

于 2016-10-21T12:36:15.257 回答