如何使用boost :: property_tree访问JSON数组?

时间:2022-04-13 23:54:58
{
    "menu": 
    {
        "foo": true,
        "bar": "true",
        "value": 102.3E+06,
        "popup": 
        [
            {"value": "New", "onclick": "CreateNewDoc()"},
            {"value": "Open", "onclick": "OpenDoc()"},
        ]
    }
}

How can I get the value of onclick?

我怎样才能获得onclick的价值?

1 个解决方案

#1


16  

Iterate through the children of the menu.popup node and extract the onclick values:

遍历menu.popup节点的子节点并提取onclick值:

void print_onclick_values(const ptree& node)
{
    BOOST_FOREACH(const ptree::value_type& child,
                  node.get_child("menu.popup")) {
        std::cout
            << "onclick: "
            << child.second.get<std::string>("onclick")
            << "\n";
    }
}

The function prints:

功能打印:

onclick: CreateNewDoc()
onclick: OpenDoc()

N.B. Delete the trailing comma from the example:

注:从示例中删除尾随逗号:

{"value": "Open", "onclick": "OpenDoc()"},

You can't access specific children of the array using a single get<string>(path) or get_child(path) call. The manual says:

您无法使用单个get (路径)或get_child(路径)调用来访问阵列的特定子级。手册说:

Depending on the path, the result at each level may not be completely determinate, i.e. if the same key appears multiple times, which child is chosen is not specified. This can lead to the path not being resolved even though there is a descendant with this path. Example:

根据路径,每个级别的结果可能不完全确定,即如果多次出现相同的键,则不指定选择哪个子级。即使存在具有此路径的后代,这也可能导致路径无法解析。例:

a -> b -> c
  -> b

The path "a.b.c" will succeed if the resolution of "b" chooses the first such node, but fail if it chooses the second.

如果“b”的分辨率选择第一个这样的节点,则路径“a.b.c”将成功,但如果选择第二个节点则失败。

The elements of the JSON array all have the empty string as name. You can access the onclick value an array element with

JSON数组的元素都具有空字符串作为名称。您可以使用带有的数组元素访问onclick值

void print_arbitrary_onclick_value(const ptree& node)
{
    std::cout << node.get<std::string>("menu.popup..onclick") << "\n";
}

but you don't know for which element the access of onclick is attempted.

但是您不知道尝试访问onclick的元素。

#1


16  

Iterate through the children of the menu.popup node and extract the onclick values:

遍历menu.popup节点的子节点并提取onclick值:

void print_onclick_values(const ptree& node)
{
    BOOST_FOREACH(const ptree::value_type& child,
                  node.get_child("menu.popup")) {
        std::cout
            << "onclick: "
            << child.second.get<std::string>("onclick")
            << "\n";
    }
}

The function prints:

功能打印:

onclick: CreateNewDoc()
onclick: OpenDoc()

N.B. Delete the trailing comma from the example:

注:从示例中删除尾随逗号:

{"value": "Open", "onclick": "OpenDoc()"},

You can't access specific children of the array using a single get<string>(path) or get_child(path) call. The manual says:

您无法使用单个get (路径)或get_child(路径)调用来访问阵列的特定子级。手册说:

Depending on the path, the result at each level may not be completely determinate, i.e. if the same key appears multiple times, which child is chosen is not specified. This can lead to the path not being resolved even though there is a descendant with this path. Example:

根据路径,每个级别的结果可能不完全确定,即如果多次出现相同的键,则不指定选择哪个子级。即使存在具有此路径的后代,这也可能导致路径无法解析。例:

a -> b -> c
  -> b

The path "a.b.c" will succeed if the resolution of "b" chooses the first such node, but fail if it chooses the second.

如果“b”的分辨率选择第一个这样的节点,则路径“a.b.c”将成功,但如果选择第二个节点则失败。

The elements of the JSON array all have the empty string as name. You can access the onclick value an array element with

JSON数组的元素都具有空字符串作为名称。您可以使用带有的数组元素访问onclick值

void print_arbitrary_onclick_value(const ptree& node)
{
    std::cout << node.get<std::string>("menu.popup..onclick") << "\n";
}

but you don't know for which element the access of onclick is attempted.

但是您不知道尝试访问onclick的元素。