boost::property_tree::ptree对象和json字符串之间互转

#include 
#include 
#include 

#include 
#include 
#include 

using namespace std;

using ptree = boost::property_tree::ptree;
//从ptree中获取json字符串
inline std::string ptreeToJsonString(const ptree& tree){
    std::stringstream ss;
    boost::property_tree::write_json(ss, tree, false);
    return ss.str();
}

//只有一层的json({}或[]表示一层)
void test0(){
    ptree obj;
    std::string buffer = std::string("{\"name\":\"Jone\",\"info\":\"INFO\"}");//from server
    
    std::stringstream ss(buffer);
    boost::property_tree::read_json(ss, obj);//从流读取到对象(这里的对象,有点像NSObject中NSArray、NSDictionary对象)
    std::cout << obj.get("name") << std::endl;
    std::cout << obj.get("info") << std::endl;
}

//只有一层的json({}或[]表示一层)
void test1(){
    ptree obj;
    std::string buffer = std::string("[1, 2, 3]");//from server
    
    std::stringstream ss(buffer);
    boost::property_tree::read_json(ss, obj);//从流读取到对象(这里的对象,有点像NSObject中NSArray、NSDictionary对象)
    
    for (auto item : obj) {
        auto num = item.second.get_value();
        cout << num << endl;
    }
}

//多层的json({}或[]表示某一层, ptree代表某一层,若内部还有层,怎可通过get_child继续获取内部层的ptree)
int parseJson()
{
  std::string str = "{\"code\":0,\"images\":[{\"url\":\"fmn057/20111221/1130/head_kJoO_05d9000251de125c.jpg\",\"num\":123},{\"url\":\"fmn057/20111221/1130/original_kJoO_05d9000251de125c.jpg\",\"num\":456}],\"dic\":{\"name\":\"lili\"}}";
  using namespace boost::property_tree;

  std::stringstream ss(str);
  ptree pt;//最外层
  try{
    read_json(ss, pt);
  }
  catch(ptree_error & e) {
    return 1;
  }

  try{
    int code = pt.get("code");   // 得到"code"的value
      if (code != 0) {
          return -1;
      }
    ptree image_array = pt.get_child("images");  // get_child得到数组对象
    
    // 遍历数组
      for (auto begin = image_array.begin(); begin != image_array.end(); ++begin) {
          ptree item = begin->second;
          std::cout << item.get("url") << std::endl;
          std::cout << item.get("num") << std::endl;
      }
      cout << "---------------------------" << endl;
      
    BOOST_FOREACH(boost::property_tree::ptree::value_type &vt, image_array) {
        ptree item = vt.second;
        std::cout << item.get("url") << std::endl;
        std::cout << item.get("num") << std::endl;
    }
      cout << "---------------------------" << endl;
      
      ptree dic = pt.get_child("dic");
      auto name = dic.get("name");
      cout << name << endl;
  }
  catch (ptree_error & e)
  {
      return 2;
  }
  return 0;
}

int updateJson()
{
  std::string str = "{\"code\":0,\"images\":[{\"url\":\"fmn057/20111221/1130/head_kJoO_05d9000251de125c.jpg\"},{\"url\":\"fmn057/20111221/1130/original_kJoO_05d9000251de125c.jpg\"}]}";
  using namespace boost::property_tree;

  std::stringstream ss(str);
  ptree pt;
  try{
    read_json(ss, pt);
  }
  catch(ptree_error & e) {
    return 1;
  }

    // 修改/增加一个key-value,key不存在则增加
    pt.put("upid", "00001");//

    cout << ptreeToJsonString(pt) << endl;

    // 插入一个数组
    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);

    cout << ptreeToJsonString(pt) << endl;

    return 0;
}


int main()
{
    updateJson();
    return 0;
}

 

你可能感兴趣的:(Server端)