问题描述
这应该很简单(我只是在学习增强功能,所以我错过了一些东西)
This should be simple (I'm just learning boost so I'm missing something)
我已经使用json_read读取了一些简单的JSON,现在有了一个ptree.网络上的所有示例均使用ptree.get("entry_name")来获取条目.我只想做些类似的事情:
I have read in some simple JSON using json_read and now have a ptree. All the examples on the web show using ptree.get("entry_name") to obtain an entry. All I want to do is something like:
ptree pt;
read_json(ss,pt);
BOOST_FOREACH(ptree::value_type &v, pt)
{
std::cout << v.{entry_name} << v.{value}
}
即遍历ptree并写出每个名称(即您在pt.get()中输入的名称)及其对应的值.
i.e. loop through the ptree and write out each name (i.e. what you put into pt.get()) and it's corresponding value.
很抱歉,这很简单
罗斯
推荐答案
我正在搜索同一件事,但找不到任何答案.事实证明确实很简单:
I was searching the same thing, and couldn't find the answer anywhere. It turned out to be pretty simple indeed:
ptree pt;
/* load/fill pt */
for(iterator iter = pt.begin(); iter != pt.end(); iter++)
{
std::cout << iter->first << "," << iter->second.data() << std::endl;
}
iter->first
是条目名称,而iter->second.data()
是第一级的条目值. (然后,您可以使用iter->second.begin()
/end()
进行重复以获取更深层次的信息.)
iter->first
is the entry name, and iter->second.data()
is the entry value of the first level. (You can then re-iterate with iter->second.begin()
/end()
for deeper levels.)
此外,如果此迭代中的一个这样的节点不是终端节点,并且本身就是ptree,则可以从此迭代器本身将其作为ptree获得:ptree subPt = iter->second.get_child("nodeName");
Further, if one such node in this iteration is not a terminal node and is itself a ptree, you can get that as ptree from this iterator itself :ptree subPt = iter->second.get_child("nodeName");
这篇关于boost:只是遍历ptree的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!