[英]How do I store a JSON key values (which I have parsed with cpp boost) in relevant data structure?
My Json has some this sort of format for now (which can of course change later) 我的Json目前有这种格式(当然以后可以更改)
{key1 : value1,
key2 : value2,
key3 :{key31 : value31,
key32 : value32,
key33 : value33
},
key4 : {key41 : value41;
key42:[ {key4a: value4a,
key4b: value4b,
key4c: {key4d: value 4d},
key4e: [v1 ,v2 ,v3]
} ,...can be more values here ]
}
}
To traverse it I am using: 要遍历它,我正在使用:
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/foreach.hpp"
void traverse(boost::property_tree::ptree pt){
using boost::property_tree::ptree;
for (ptree::value_type &v : pt)
{
std::cout<<v.first<<" - "<<v.second.data()<<std::endl;
if (v.second.size() >= 1){
traverse(v.second);
}
}
}
With this I am able to touch each and every node of my Json. 有了这个,我就能触及Json的每个节点。 I am looking for a better approach to parse and store the Json Key:values now. 我正在寻找一种更好的方法来解析和存储Json Key:values。
I think you underestimate the ability of property tree. 我认为您低估了属性树的能力。 It's a great tool to store and access json and xml like infos and that's why it's introduced with json_parser and xml_parser in boost. 这是一个像信息一样存储和访问json和xml的好工具,这就是为什么在boost中将json_parser和xml_parser引入的原因。
Here Accessing values using a boost::property_tree::string_path in the question you can see a working example of how to access values in property tree with path-like strings. 在此问题中, 使用boost :: property_tree :: string_path访问值时,您可以看到一个有效的示例,该示例说明如何使用类似路径的字符串访问属性树中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.