簡體   English   中英

如何在boost屬性樹中編寫嵌套列表並將其轉儲為json?

[英]How to write nested list in boost property tree and dump it as json?

我想在 boost json 中使用嵌套列表。 我想要的是

{"matrix": [[0.0, 0.0], [0.0, 0.0]]}

我在官方文檔中找不到嵌套列表案例

文檔對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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM