0

related to Using boost property tree to read int array

I want to use boost (1.53.0) to read this:

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

but the properties of the objects inside the array model_actions won't get printed!

my code:

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());
}

the printout:

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

why? what's wrong with the printout 2.? why is it different from the printout at 1.?

ihadanny
  • 4,377
  • 7
  • 45
  • 76

1 Answers1

1

JSON arrays are parsed into 'unnamed' subtrees; hence this

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

will have empty strings as key and data; if you want its children, you should write:

MLOG("\t2. [%s][%s]\n", attr.first.c_str(), attr.second.data().c_str());
sehe
  • 374,641
  • 47
  • 450
  • 633
Massimiliano Janes
  • 5,524
  • 1
  • 10
  • 22