boost::property_tree::ptree 创建和解析json数组_boost ptree json

boost::property_tree::ptree 对简单的json处理我觉得还可以,对于数组的处理网上有些代码可能不适合新版本的boost。比如push_back(); 第二个参数很多代码都是字符串,可是我目前使用的代码要求第二个参数必须是boost::property_tree::ptree,如果你用字符串报错,那么可以试试我的方法。

头文件

  1. #include <boost/property_tree/ptree.hpp>
  2. #include <boost/property_tree/json_parser.hpp>

创建json 数组

  1. boost::property_tree::ptree array;
  2. boost::property_tree::ptree pt1;
  3. pt1.put("", "xhh");
  4. boost::property_tree::ptree pt2;
  5. pt1.put("", "cy");
  6. array.push_back(std::make_pair("",pt1));
  7. array.push_back(std::make_pair("",pt2));

解析json 数组

  1. boost::property_tree::ptree nameItem;
  2. for (boost::property_tree::ptree::iterator it = array.begin(); it != array.end(); ++it)
  3. {
  4. nameItem = it->second;
  5. std::cout<<nameItem.get_value<std::string>()<<std::endl;
  6. }