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.?

1
  • can you clarify what's the wanted result ? Commented Jan 7, 2018 at 13:44

1 Answer 1

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());
1
  • oops. brain-fart :( mindless copy-pasta and forgot to replace p with attr.
    – ihadanny
    Commented Jan 7, 2018 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.