【问题标题】:using boost for reading complex JSON arrays使用 boost 读取复杂的 JSON 数组
【发布时间】:2018-01-07 13:29:05
【问题描述】:

Using boost property tree to read int array相关

我想用 boost (1.53.0) 来阅读这个:

{
  "model_json_version": "333",
  "model_actions": [
    {
      "action_type": "rep_processor",
      "rp_type": "basic_cln"
    },
    {
      "action_type": "feat_generator",
      "tags": "numeric"
    }
  ]
}

但是数组model_actions 内的对象的属性不会被打印出来!

我的代码:

for (ptree::value_type &p : pt)
    MLOG("1. [%s][%s]\n", p.first.c_str(), p.second.data().c_str());
for (ptree::value_type &p : pt.get_child("model_actions")) {
    auto& action = p.second;
    MLOG("\taction_type [%s]\n", action.get<string>("action_type").c_str());
    for (ptree::value_type &attr : action)
        MLOG("\t2. [%s][%s]\n", p.first.c_str(), p.second.data().c_str());
}

打印输出:

1. [model_json_version][333]
1. [model_actions][]
    action_type [rep_processor]
    2. [][]
    2. [][]
    action_type [feat_generator]
    2. [][]
    2. [][]

为什么?打印输出2. 有什么问题?为什么它与1. 的打印输出不同?

【问题讨论】:

  • 你能澄清一下想要的结果是什么吗?

标签: c++ arrays json boost


【解决方案1】:

JSON 数组被解析为“未命名”子树;所以这个

MLOG("\t2. [%s][%s]\n", p.first.c_str(), p.second.data().c_str());

将有空字符串作为键和数据;如果你想要它的孩子,你应该写:

MLOG("\t2. [%s][%s]\n", attr.first.c_str(), attr.second.data().c_str());

【讨论】:

  • 哎呀。 Brain-fart :( 盲目的复制意大利面,忘记用 attr 替换 p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
相关资源
最近更新 更多