4

I have some JSON data collected via boost and I can not work out how to access some of the data that is in an array:

JSON data : {"dvm_gnd": {"num" : 4, "value": [1,2,3,4]}, "xx_gn" : {"num : 1, "value": 5}}

I can easily get the "num" and single "value" (5) out using:

BOOST_FOREACH(ptree::value_type &v, pt) {
  float value = v.second.get<float>("value")
}

However I have no idea how to access the elements of the array out? What does the ptree.get() return?

Thanks

Ross

Ross W
  • 1,300
  • 3
  • 14
  • 24

2 Answers2

3

Try this:

BOOST_FOREACH(ptree::value_type &v, pt.get_child("dvm_gnd.value")) {
  float value = v.second.data();
}
Tralamazza
  • 316
  • 2
  • 5
2

I'm sure you've moved on by now, but in case someone else comes across this, ptree puts those array values as children with a blank name, so the code you want looks something like:

BOOST_FOREACH(const ptree::value_type &v, pt.get_child("dvm_gnd.value")) {
  float value = v.second.get<float>("");
}

Or you can use the optional or default value version of get

stokastic
  • 986
  • 2
  • 7
  • 19