我正在使用 boost 库来操作 JSON 字符串,并且我想访问第一个元素。
我想知道是否有一些方便的方法可以访问没有路径名的 ptree 的第一个元素。
我这样做,但我没有任何价值:
namespace pt = boost::property_tree;
pt::ptree pt2;
string json = "\"ok\"";
istringstream is(json);
try
{
pt::read_json(is, pt2);
cout << pt2.get_child("").equal_range("").first->first.data() << endl;
}
catch (std::exception const& e)
{
cerr << e.what() << endl;
}
解决方案:
代替cout << pt2.get_child("").equal_range("").first->first.data() << endl;
经过cout << pt2.get_value<std::string>() << endl;