![](/img/trans.png)
[英]Boost read_json and write_json are not equipollent for property tree
[英]How to write nested list in boost property tree and dump it as json?
文檔對create list value不是很詳細,我已經嘗試完成了,需要用空路徑調用put
用空路徑調用push_back
int main() {
pt::ptree child_inner1;
pt::ptree child_inner2;
child_inner1.put("", 0.0);
child_inner2.put("", 0.0);
pt::ptree child;
child.push_back(std::make_pair("", child_inner1));
child.push_back(std::make_pair("", child_inner2));
pt::ptree sub;
sub.push_back(std::make_pair("", child));
sub.push_back(std::make_pair("", child));
pt::ptree tree;
tree.add_child("matrix", sub);
std::ostringstream oss;
pt::write_json(oss, tree);
std::cout << oss.str();
return 0;
}
代碼有點丑。 所以我建議使用其他 JSON 庫來生成 JSON。 由於您使用的是 boost,最新版本的 boost 已經有一個超快的 JSON 庫,而且采用現代設計,這將是一個好的開始: https : //www.boost.org/doc/libs/1_75_0/libs /json/doc/html/index.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.