Frage

Ich sehe keinen Weg, um ein Array mit boost :: Eigenschaftsbaum zu erstellen. Der folgende Code ...

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include <iostream>

int main()
{
  try {
    boost::property_tree::ptree props;
    props.push_back(std::make_pair("foo", "bar"));
    props.push_back(std::make_pair("foo", "baz"));
    boost::property_tree::write_json("prob.json", props);
  } catch (const std::exception & ex) {
    std::cout << ex.what() << std::endl;
  }
}

... gibt mir nur ...

{
  "foo": "bar",
  "foo": "baz"
}

Die docs auf boost :: property_tree sind spärlich. Wie erstelle ich einen JSON-Array mit boost :: property_tree?

War es hilfreich?

Lösung

Wenn Sie einen Unterbaum, dessen einzige Knoten haben leere Schlüssel, dann wird es als ein Array serialisiert werden:

boost::property_tree::ptree array;
array.push_back(std::make_pair("", "bar"));
array.push_back(std::make_pair("", "baz"));

boost::property_tree::ptree props;
props.push_back(std::make_pair("array", array));

boost::property_tree::write_json("prob.json", props);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top