BOOST JSON 数组解析_boost json 解析数组

JSON消息格式:

{“jobIDs”:

        ["24324325435",

          "ew32trt4ty45yt45"

         ]

}


filePath 存储JSON消息的文件

  1. int CParseJson::readBoostJson(string& filePath, vector<string>& jobID)
  2. {
  3. int ret = 0;
  4. ptree ptJobIDs, ptChild, readPt;
  5. read_json(filePath, readPt);
  6. if(readPt.count("jobIDs"))
  7. {
  8. ptree ptChildRead = readPt.get_child("jobIDs");
  9. for(BOOST_AUTO(pos, ptChildRead.begin()); pos != ptChildRead.end(); ++pos)
  10. {
  11. string job = pos->second.get_value<string>();
  12. jobID.push_back(job);
  13. }
  14. }
  15. else
  16. {
  17. LOG4CXX_DEBUG(logger, "no delete msg!");
  18. }
  19. return ret;
  20. }
  21. int CParseJson::writeBoostJson(string jobid, string& filePath)
  22. {
  23. int ret= 0 ;
  24. ptree ptJobIDs, ptChild, readPt;
  25. read_json(filePath, readPt);
  26. if(readPt.count("jobIDs"))
  27. {
  28. ptree ptChildRead = readPt.get_child("jobIDs");
  29. /*for(BOOST_AUTO(pos, ptChildRead.begin()); pos != ptChildRead.end(); ++pos)
  30. {
  31. string job = pos->second.get_value<string>();
  32. jobID.push_back(job);
  33. }*/
  34. readPt.erase("jobIDs");
  35. write_json(filePath, readPt);
  36. ptChildRead.push_back(make_pair("", jobid));
  37. readPt.push_back(make_pair("jobIDs", ptChildRead));
  38. write_json(filePath, readPt);
  39. }
  40. else
  41. {
  42. ptChild.push_back(make_pair("", jobid));
  43. ptJobIDs.push_back(make_pair("jobIDs", ptChild));
  44. write_json(filePath, ptJobIDs);
  45. }
  46. return ret;
  47. }