我需要你关于nlohmann的json库的帮助。这个代码是我写的。
std::vector<std::uint8_t> v = nlohmann::json::to_bson(jInit);
std::vector<std::uint8_t> s;
for (auto& i : v)
{
s.push_back(v[i]);
}
std::ofstream ofs(s_basePath + "dump.txt");
ofs << nlohmann::json::from_bson(s).dump();
这只需将json转换为bson,然后将bson转换为json并将其转储为文本文件。
由于某些原因,我不得不使用push_back((来获取json。
问题是,转储的文本文件的大小是0kb,不会转储任何内容。
我也试过这个代码,它正在工作。
std::vector<std::uint8_t> v = nlohmann::json::to_bson(jInit);
std::ofstream ofs(s_basePath + "dump.txt");
ofs << nlohmann::json::from_bson(v).dump();
我不知道向量v和s之间有什么区别。
问题与json/bson无关。只是你使用原始bson数据中的每个uint8_t
作为对相同数据的索引,这会打乱一切。
错误:
for (auto& i : v)
{
s.push_back(v[i]); // `i` is not an index
}
正确的循环应该是这样的:
for (auto& i : v)
{
s.push_back(i); // `i` is a reference to the actual data you should put in `s`
}
如果你需要一个带有bson数据的矢量副本,你可以简单地复制它:
s = v;