访问Boost PropertyTree(JSON)中列表的特定元素

时间:2016-11-16 14:25:45

标签: c++ json list boost element

我有这样的数据

{"json": "a_string", "data": [0,1,2,3,4]}

我正在尝试访问"数据"的任何特定元素。 "直接"

受到迭代遍历元素的方法的启发,如here所述,我使用迭代器来获取第一个元素。

root.get_child("data").begin()->second.get_value<int>();

但是我很确定应该有一个选项可以说出.get(0)或.get(1)之类的内容,但是这个调用需要我在列表/向量之类的操作上运行的知识。

这是我的代码:

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

using namespace std;
using namespace boost::property_tree;

int main(int argc, char** argv)
{
    string str("{\"json\": \"a_string\", \"data\": [0,1,2,3,4]}");
    stringstream ss(str);
    ptree root;
    read_json(ss,root);

    int first_value = root.get_child("data").begin()->second.get_value<int>();

    cout << first_value << endl;
    getc(stdin);
}

0 个答案:

没有答案