我需要你關于 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。
問題是,轉儲文本檔案的大小為 0 kb,不會轉儲任何內容。
我也試過這段代碼,它正在作業。
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 有什么區別。
uj5u.com熱心網友回復:
問題無關json/布森. 只是你uint8_t在原版中使用了every布森資料作為索引到相同的資料中,這會將所有內容都打亂。
錯誤的:
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`
}
如果您需要帶有布森資料,你可以簡單地復制它:
s = v;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/457296.html
標籤:C json nlohmann-json
