boost库中property_tree
0.头文件
相关头文件为:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace boost::property_tree;
1.读Json字符串
1.1 读简单值
auto description = pt.get<std::string>("description");
auto version = pt.get<int>("version");
1.2 读一组对象
for (auto ptItem : pt.get_child("list"))
{
std::string key = ptItem.first;
std::string val = ptItem.second.data();
}
1.3 数组中包含对象解析
# test.json文件
{
"code":"0",
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
boost::property_tree::ptree pt, temp_pt;
boost::property_tree::json_parser::read_json("test.json", pt);
boost::property_tree::ptree elements = pt.get_child("employees");
boost::property_tree::ptree::iterator json_iterator = elements.begin();
for (json_iterator == elements.begin(); json_iterator != elements.end(); json_iterator++)
{
auto m_wing_area = json_iterator->second.get<string>("firstName");
cout << "m_wing_area is: " << m_wing_area << endl;
}
2.写Json字符串
2.1 将ptree转换成字符串输出到控制台
std::stringstream ss;
boost::property_tree::write_json(ss, pt);
std::string strContent = ss.str();
2.2 添加简单值
boost::property_tree::ptree root;
root.put(std::string("description"), std::string("this is a JSON test"));
root.put(std::string("version"), 100);
2.3 添加一组对象
boost::property_tree::ptree ptObjs;
ptObjs.put(std::string("listkey1"), std::string("listvalue1"));
ptObjs.put(std::string("listkey2"), std::string("listvalue2"));
ptObjs.put(std::string("listkey3"), std::string("listvalue3"));
root.add_child(std::string("list"), ptObjs);
property_tree库的核心类是basic_ptree,它有两个重要的内部类型定义self_type和value_type。其中value_type是节点数据结构,它是一个std::pair,含有节点的属性名(first)和节点自身(second)。
// 插入一个数组
ptree exif_array;
ptree element1, element2, element3;
element1.put("Make", "NIKON");
element1.put("buildingcode", 100);
element2.put("DateTime", "2011:05:31 06:47:09");
element3.put("Software", "Ver.1.01");
exif_array.push_back(std::make_pair("", element1));//将这一层作为数组中的子元素,即添加一个子ptree
exif_array.push_back({ "", element2 });//将这一层作为数组中的子元素,即添加一个子ptree
exif_array.push_back(std::make_pair("", element3));//将这一层作为数组中的子元素,即添加一个子ptree
pt.put_child("exifs", exif_array);
3. 示例
test.json
{
"code":"0",
"msg":"successful!",
"body":{
"data":[{"000":"111"}, {"000":"222"}, {"000":"333"}],
"jaminliu":"helloWorld"
}
}
示例代码:
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
using namespace std;
using namespace boost::property_tree;
int main()
{
boost::property_tree::ptree pt, temp_pt;
boost::property_tree::json_parser::read_json("test.json", pt);
string temp_code = pt.get<string>("code");
string temp_msg = pt.get<string>("msg");
cout << "code is: " << temp_code << endl;
cout << "msg is: " << temp_msg << endl;
auto temp_data = pt.get_child("body.data");
for (auto &data : temp_data)
{
auto temp_data_2 = data.second.get<string>("000");
cout << temp_data_2 << " ";
}
cout << endl;
auto temp_jaminliu = pt.get_child("body.jaminliu");
cout << temp_jaminliu.get_value<string>() << endl;
pt.put("body.jaminliu", "SZSE");
auto temp_jaminliu2 = pt.get_child("body.jaminliu");
cout << temp_jaminliu2.get_value<string>() << endl;
stringstream temp;
write_json(temp, pt);
cout << temp.str() << endl;
system("pause");
return 0;
}
运行结果: