json+c++

来源:互联网 发布:面试美工提问哪些问题 编辑:程序博客网 时间:2024/05/17 04:24

boost生成和解析json实例及定向到文件


添加boost:

1.点击还原NuGet包 

1.


2.点击第一个安装



3. cpp代码中加载头文件即可


生成json

[plain] view plain copy
  1. // test.cpp  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <boost/property_tree/ptree.hpp>  
  5. #include <boost/property_tree/json_parser.hpp>  
  6. #include <boost/foreach.hpp>  
  7. using namespace std;  
  8. using namespace boost::property_tree;  
  9.    
  10. int main(){  
  11.     ptree pt_1,pt_11,pt_12;  
  12.      
  13.     pt_11.put("id","3445");  
  14.     pt_11.put<int>("age",29);  
  15.     pt_11.put("name","chen");     
  16.      
  17.     pt_12.push_back(make_pair("",pt_11));  
  18.     pt_12.push_back(make_pair("",pt_11));  
  19.     //replace or create child node "data"  
  20.     pt_1.put_child("data",pt_12);     
  21.     ostringstream os;  
  22.     write_json(os,pt_1);  
  23.     cout<<os.str()<<endl;  
  24.     system("pause");  
  25.     return 0;  
  26. }  
  27. // =========== 产生如下JSON串: ===============  
  28.     /*  
  29.     {  
  30.     "data":  
  31.         [  
  32.             {  
  33.                 "id": "3445",  
  34.                 "age": "29",  
  35.                 "name": "chen"  
  36.             },  
  37.             {  
  38.                 "id": "3445",  
  39.                 "age": "29",  
  40.                 "name": "chen"  
  41.             }  
  42.         ]  
  43.     }  
  44.     */  

生成json
格式为:

#include <iostream>  #include <string>  #include <boost/property_tree/ptree.hpp>  #include <boost/property_tree/json_parser.hpp>  #include <boost/foreach.hpp> #include<opencv2/opencv.hpp> #include<vector>using namespace std;using namespace cv;using namespace boost::property_tree;int main() {//ptree pt_1, pt_11, pt_12;//pt_11.put("id", "3445");//pt_11.put<int>("age", 29);//pt_11.put("name", "chen");//pt_12.push_back(make_pair("", pt_11));//pt_12.push_back(make_pair("", pt_11));//pt_12.push_back(make_pair("", pt_11));////replace or create child node "data"  //pt_1.put_child("data", pt_12);//ostringstream os;//write_json(os, pt_1);//cout << os.str() << endl;//system("pause");//return 0;vector<Point> points;Point point1 = { 1,2 };Point point2 = { 3,4 };points.push_back(point1);points.push_back(point2);ptree pt_1,  pt_12,pt_121,pt_1211, pt_122;int a = 20;pt_1.put<int>("code", a);//pt_1.put("data", pt_12);pt_121.put("url", "h");pt_121.put<int>("people_num", 2);pt_121.put<int>("is_high_density", 0);//pt_122.put<int>("is_high_density", 0);//pt_121.put<int>("x", points[0].x);for (int i = 0; i < points.size(); i++) {pt_1211.put<int>("x", points[i].x);pt_1211.put<int>("y", points[i].y);//pt_121.put("x","y", points[i]);//pt_121.put<int>("y", points[i].y);//cout << points[i].x;//cout << points[i].y;pt_122.push_back(make_pair("", pt_1211));}//pt_122.push_back(make_pair("", pt_1211));//pt_122.push_back(make_pair("", pt_1211));pt_121.put_child("people_point", pt_122);pt_12.push_back(make_pair("", pt_121));//pt_122.push_back(make_pair("", pt_1211));pt_1.put_child("data", pt_12);//pt_121.put_child("people_point", pt_122);ostringstream os;write_json(os, pt_1);cout << os.str() << endl;system("pause");return 0;}
运行截图:



[plain] view plain copy
  1. #include <boost/property_tree/ptree.hpp>  
  2. #include <boost/property_tree/json_parser.hpp>  
  3. #include <boost/foreach.hpp>  


[plain] view plain copy
  1. void downgadget::Downloader_c::makereport(const downloaderInfo_t info)  
  2. {  
  3.     using boost::property_tree::ptree;  
  4.     ptree downloaderInfo ;  
  5.   
  6.   
  7.     downloaderInfo.put("id",info.id);  
  8.     downloaderInfo.put("code",info.code);  
  9.     downloaderInfo.put("mesg",info.mesg);  
  10.     downloaderInfo.put("path",info.path);  
  11.     downloaderInfo.put("md5sum",info.md5sum);  
  12.     downloaderInfo.put("size",info.size);  
  13.     downloaderInfo.put("url",info.url);  
  14.     downloaderInfo.put("time_download",info.time_download);  
  15.     downloaderInfo.put("time_install",info.time_install);  
  16.     downloaderInfo.put("cmd_install",info.cmd_install);  
  17.   
  18.     std::ostringstream os;  
  19.     write_json(os,downloaderInfo);  
  20.     std::cout<<os.str()<<std::endl;  
  21.   
  22.     std::ofstream tmpReport(m_reportPath);  
  23.     if(!tmpReport){  
  24.         std::cout<<"critical error open output report error!"<<std::endl;  
  25.     }else{  
  26.         tmpReport<<os.str();  
  27.     }  
  28.     tmpReport.close();  
  29. }  



已知 json 直接遍历所有:

[plain] view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <boost/property_tree/ptree.hpp>  
  4. #include <boost/property_tree/json_parser.hpp>  
  5. #include <boost/foreach.hpp>  
  6. using namespace std;  
  7. using namespace boost::property_tree;  


[plain] view plain copy
  1. string json_string="{\"-f\": \"/usr/reservedfff_dir\", \"-s\": \"/usr/reservedddd_dir\"}";  
  2.   
  3. string str_head;  
  4. string str_node_val;  
  5. ptree pt,p1,p2;  
  6.   
  7. stringstream stream;  
  8. stream << json_string ;  
  9. read_json<ptree>( stream, pt);  
  10.   
  11. for (ptree::iterator ita = pt.begin(); ita != pt.end(); ++ita)  
  12. {  
  13.     cout<<"first:"<<ita->first<<endl;  
  14.     str_node_val = pt.get<string>(ita->first);  
  15.     cout<<str_node_val<<endl;  
  16. }  

加入exception:

[plain] view plain copy
  1. string json_string="{\"-f\": \"/usr/reservedfff_dir\" \"-s\": \"/usr/reservedddd_dir\"}";  
  2.   
  3. string str_head;  
  4. string str_node_val;  
  5. ptree pt,p1,p2;  
  6.   
  7. stringstream stream;  
  8. stream << json_string ;  
  9.   
  10.   
  11. try{  
  12.     read_json<ptree>( stream, pt);  
  13.     cout<<"parsing ok\n"<<endl;  
  14.     for (ptree::iterator ita = pt.begin(); ita != pt.end(); ++ita)  
  15.     {  
  16.         cout<<"first:"<<ita->first<<endl;  
  17.         str_node_val = pt.get<string>(ita->first);  
  18.         cout<<str_node_val<<endl;  
  19.     }  
  20. }  
  21. catch(std::runtime_error& e)  
  22. {  
  23.     std::cout<<e.what()<<endl;  
  24. }