64,677
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include <string>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
//解析下面的json
int main()
{
using namespace boost::property_tree;
std::string strJson = "{ \"people\": [{\"heh\":{ \"firstName\": \"Brett\",\"lastName\":\"McLaughlin\", \"email\": \"aaaa\" }},"
"{\"ab\":{ \"firstName\": \"Jason\", \"lastName\":\"Hunter\",\"email\": \"bbbb\"}},"
"{\"cd\":{\"firstName\": \"Elliotte\", \"lastName\":\"Harold\", \"email\": \"cccc\" }}]}";
ptree pt;
std::stringstream ss(strJson);
read_json<ptree>(ss, pt);//读取到pt中去
ptree people;
people = pt.get_child("people");
for (auto p : people)
{
//我想知道这个first到底是什么内容,我以为是上面的"heh","ab","cd",但不能打印出来,那怎么打印这个first
//刚开始这样的
//"{ \"people\": [{ \"firstName\": \"Brett\",\"lastName\":\"McLaughlin\", \"email\": \"aaaa\" },"
//"{ \"firstName\": \"Jason\", \"lastName\":\"Hunter\",\"email\": \"bbbb\"},"
//"{\"firstName\": \"Elliotte\", \"lastName\":\"Harold\", \"email\": \"cccc\" }]}";
std::cout << p.first << std::endl;
}
getchar();
return 0;
}
std::string json = "{\"A\":1,\"B\":{\"C\":2,\"D\":3},\"E\":[{\"F\":4},{\"F\":5}]}";
boost::property_tree::ptree pt,child1,child2 ;
std::stringstream ss(json) ;
boost::property_tree::read_json(ss, pt);
child1 = pt.get_child("B");
//针对树遍历
for(auto c:child1)
{
cout<< c.first<<c.second.data()<<endl;//这样可以打印出first
}