【发布时间】:2015-07-18 07:12:14
【问题描述】:
我现在已经尝试了太多时间,从 boost 库中访问 json_reader ptree。
我有一个经常被封装的 json 文件:(pseudo-json:)
"Foo": {
"nameofFoo:"foofoo"
"Bar": [{
"BarFoo":
{ BarFooDeep: {
BarFooDeepDeep: {
"BarFooValue1": 123
"BarFooValue2" : 456
}
}
}
"FooBar": [ {
"FooBarDeep" :[ {
FooBarDeepDeep:[ {
FooBarValue1: "ineedthis"
FooBarValue2: "andthis"
} ]
FooBarDeepDeep1:[ {
FooBarValue1: "ineedthis"
FooBarValue2: "andthis"
} ]
"FooBarDeep" :[ {
FooBarDeepDeep2:[ {
FooBarValue1: "ineedthis"
FooBarValue2: "andthis"
} ]
FooBarDeepDeep3:[ {
FooBarValue1: "ineedthis"
FooBarValue2: "andthis"
} ]
and so on .... won t complete this now...
现在我只需要获取所有FooBar的FooBarValue1和FooBarValue2。
我知道 ptree 将数组与空子 ("") 放在一起
我可以通过递归遍历所有子节点来访问所有成员。
但是没有更好的方法来访问特殊值吗?
ptree find 是如何工作的?我总是遇到编译器错误...
ptree jsonPT;
read_json( JSON_PATH, jsonPT);
ptree::const_iterator myIT = jsonPT.find("FooBarValue1");
double mlat = boost::lexical_cast<int>(myIT->second.data());
错误:从 'boost::property_tree::basic_ptree, std::basic_string >::assoc_iterator' 到非标量类型 'boost::property_tree::basic_ptree, std::basic_string >::const_iterator' 请求 ptree::const_iterator myIT = jsonPT.find("FooBarValue1");
谁能给我一个有用的提示如何访问这个 ptree?!?
【问题讨论】:
-
你能让 JSON 更合理吗?实在是太崩溃了,什么都说不出来
-
您可以在此处查看路径算法以获得灵感stackoverflow.com/a/29199812/85371(处理与 JSON 相同,因为它只适用于 ptree)
-
编辑没有使 json 有效(甚至没有关闭)。我已经为您完成了,请参阅我的答案:/