rapidjson读写测试

来源:互联网 发布:穿越时光的地铁知乎 编辑:程序博客网 时间:2024/06/05 05:47
  1. #include <iostream>  
  2. #include <string>  
  3. #include <fstream>  
  4. //包含rapidjson必要头文件,rapidjson文件夹拷贝到工程目录,或者设置include路径,或者加入到工程树  
  5. #include "rapidjson/document.h"  
  6. #include "rapidjson/filestream.h"  
  7. #include "rapidjson/prettywriter.h"  
  8. #include "rapidjson/stringbuffer.h"  
  9. using namespace std;  
  10. using namespace rapidjson;  //引入rapidjson命名空间  
  11.   
  12. //写json文件  
  13. void json_write()  
  14. {  
  15.     Document doc;  
  16.     doc.SetObject();  
  17.     Document::AllocatorType &allocator=doc.GetAllocator(); //获取分配器  
  18.     //1.添加字符串对象  
  19.     doc.AddMember("author","tashaxing",allocator);   
  20.     //2.添加数组对象  
  21.     Value array1(kArrayType);  
  22.     for(int i=0;i<3;i++)  
  23.     {  
  24.         Value int_object(kObjectType);  
  25.         int_object.SetInt(i);  
  26.         array1.PushBack(int_object,allocator);  
  27.     }  
  28.     doc.AddMember("number",array1,allocator);  
  29.     //3.添加复合对象  
  30.     Value object(kObjectType);  
  31.     object.AddMember("language1","C++",allocator);  
  32.     object.AddMember("language2","java",allocator);  
  33.     doc.AddMember("language",object,allocator);  
  34.     //4.添加对象数组和复合对象的组合  
  35.     Value array2(kArrayType);  
  36.     Value object1(kObjectType);  
  37.     object1.AddMember("hobby","drawing",allocator);  
  38.     array2.PushBack(object1,allocator);  
  39.     Value object2(kObjectType);  
  40.     object2.AddMember("height",1.71,allocator);  
  41.     array2.PushBack(object2,allocator);  
  42.     doc.AddMember("information",array2,allocator);  
  43.     StringBuffer buffer;  
  44.     PrettyWriter<StringBuffer> pretty_writer(buffer);  //PrettyWriter是格式化的json,如果是Writer则是换行空格压缩后的json  
  45.     doc.Accept(pretty_writer);  
  46.     //打印到屏幕  
  47.     cout<<"the json output:"<<endl;  
  48.     cout<<buffer.GetString()<<endl;  
  49.     //输出到文件  
  50.     ofstream fout;  
  51.     fout.open("test");    //可以使绝对和相对路径,用\\隔开目录,test, test.json, test.txt 都行,不局限于文件格式后缀,只要是文本文档  
  52.     fout<<buffer.GetString();  
  53.     fout.close();  
  54. }  
  55.   
  56. //读json文件  
  57. void json_read()  
  58. {  
  59.     cout<<"the json read:"<<endl;  
  60.     ifstream fin;  
  61.     fin.open("test");  
  62.     string str;  
  63.     string str_in="";  
  64.     while(getline(fin,str))    //一行一行地读到字符串str_in中  
  65.     {  
  66.         str_in=str_in+str+'\n';  
  67.     }  
  68.     //解析并打印出来  
  69.     Document document;  
  70.     document.Parse<0>(str_in.c_str());  
  71.   
  72.     Value &node1=document["author"];  
  73.     cout<<"author: "<<node1.GetString()<<endl;  
  74.   
  75.     Value &node2=document["number"];  
  76.     cout<<"number: "<<endl;  
  77.     if(node2.IsArray())  
  78.     {  
  79.         for(int i=0;i<node2.Size();i++)  
  80.             cout<<'\t'<<node2[i].GetInt()<<endl;  
  81.     }  
  82.   
  83.     Value &node3=document["language"];  
  84.     cout<<"language: "<<endl;  
  85.     Value &tmp=node3["language1"];  
  86.     cout<<'\t'<<"language1: "<<tmp.GetString()<<endl;  
  87.     tmp=node3["language2"];  
  88.     cout<<'\t'<<"language2: "<<tmp.GetString()<<endl;  
  89.   
  90.     Value &node4=document["information"];  
  91.     cout<<"information: "<<endl;  
  92.     if(node4.IsArray())  
  93.     {  
  94.         int i=0;  
  95.         Value &data=node4[i];   //注意,此处下表索引只能用变量,不能用常量,例如node[0]编译错误  
  96.         cout<<'\t'<<"hobby: "<<data["hobby"].GetString()<<endl;  
  97.         i=1;  
  98.         data=node4[i];  
  99.         cout<<'\t'<<"height: "<<data["height"].GetDouble()<<endl;  
  100.     }  
  101.   
  102. }  
  103. int main(int argc,char **argv)  
  104. {  
  105.     //写、读 测试  
  106.     json_write();  
  107.     json_read();  
  108.     return 0;  
原创粉丝点击