一,字符串json封裝及及解析的實例
#include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include <iostream> using namespace rapidjson; using namespace std; int main() { //一,轉json格式 //1,獲取Document對象 Document doc; doc.SetObject(); //key-value 相當與map //doc.Setvalue(); //數組型 相當與vector Document::AllocatorType &allocator=doc.GetAllocator(); //獲取分配器 //2,給doc對象賦值 doc.AddMember("name","張山",allocator); //添加數組型數據 Value array1(kArrayType); for(int i=0;i<3;i++) { Value int_object(kObjectType); int_object.SetInt(i); array1.PushBack(int_object,allocator); } doc.AddMember("number",array1,allocator); //3,將doc對象的值寫入字符串 StringBuffer buffer; //PrettyWriter<StringBuffer> writer(buffer); //PrettyWriter是格式化的json,如果是Writer則是換行空格壓縮后的json Writer<StringBuffer> writer(buffer); doc.Accept(writer); cout<<buffer.GetString()<<endl; //二,解析json格式 //1,將json格式字符串轉換 string readdate; readdate = buffer.GetString(); Document document; document.Parse<0>(readdate.c_str()); //2,取出自己想要的值 Value &node1=document["name"]; cout<<"name:"<<node1.GetString()<<endl; Value &node2=document["number"]; cout<<"number: "<<endl; if(node2.IsArray()) { for(int i=0;i<node2.Size();i++) cout<<'\t'<<node2[i].GetInt()<<endl; } return 0; }
二,文件json封裝及解析的實例
1 兩個問題
(1)標准json和非標准json:
標准json要求鍵必須都是雙引號的字符串,而非標准json可以單引號。
例如:
{a : 'abc'}
{'a' : 'abc'}
{a : "abc"}
{"a" : "abc"}
只有第4個是標准json
(2)json中的[]與{}:
在 JSON 里 [] 是 Array {} 是Ojbect
[] Array 的key 是 int 從0算起
{} 的key 是 string
var a= Array();
a[a.length] = '3';
a[a.length] = '4';
a[a.length] = '5';
a toJSON 后 ='["3", "4", "5"]'
var a = new Object();
a['test1'] = '3';
a['test2'] = '4';
a['test3'] = '5';
a toJSON 后 = '{"test1":"3", "test2":"4", "test3":"5"}'
2 rapidjson讀寫測試
下載rapidjson庫,解壓后關聯到工程。
代碼:
#include <iostream> #include <string> #include <fstream> //包含rapidjson必要頭文件,rapidjson文件夾拷貝到工程目錄,或者設置include路徑,或者加入到工程樹 #include "rapidjson/document.h" #include "rapidjson/filestream.h" #include "rapidjson/prettywriter.h" #include "rapidjson/stringbuffer.h" using namespace std; using namespace rapidjson; //引入rapidjson命名空間 //寫json文件 void json_write() { Document doc; doc.SetObject(); Document::AllocatorType &allocator=doc.GetAllocator(); //獲取分配器 //1.添加字符串對象 doc.AddMember("author","tashaxing",allocator); //2.添加數組對象 Value array1(kArrayType); for(int i=0;i<3;i++) { Value int_object(kObjectType); int_object.SetInt(i); array1.PushBack(int_object,allocator); } doc.AddMember("number",array1,allocator); //3.添加復合對象 Value object(kObjectType); object.AddMember("language1","C++",allocator); object.AddMember("language2","java",allocator); doc.AddMember("language",object,allocator); //4.添加對象數組和復合對象的組合 Value array2(kArrayType); Value object1(kObjectType); object1.AddMember("hobby","drawing",allocator); array2.PushBack(object1,allocator); Value object2(kObjectType); object2.AddMember("height",1.71,allocator); array2.PushBack(object2,allocator); doc.AddMember("information",array2,allocator); StringBuffer buffer; PrettyWriter<StringBuffer> pretty_writer(buffer); //PrettyWriter是格式化的json,如果是Writer則是換行空格壓縮后的json doc.Accept(pretty_writer); //打印到屏幕 cout<<"the json output:"<<endl; cout<<buffer.GetString()<<endl; //輸出到文件 ofstream fout; fout.open("test"); //可以使絕對和相對路徑,用\\隔開目錄,test, test.json, test.txt 都行,不局限於文件格式后綴,只要是文本文檔 fout<<buffer.GetString(); fout.close(); } //讀json文件 void json_read() { cout<<"the json read:"<<endl; ifstream fin; fin.open("test"); string str; string str_in=""; while(getline(fin,str)) //一行一行地讀到字符串str_in中 { str_in=str_in+str+'\n'; } //解析並打印出來 Document document; document.Parse<0>(str_in.c_str()); Value &node1=document["author"]; cout<<"author: "<<node1.GetString()<<endl; Value &node2=document["number"]; cout<<"number: "<<endl; if(node2.IsArray()) { for(int i=0;i<node2.Size();i++) cout<<'\t'<<node2[i].GetInt()<<endl; } Value &node3=document["language"]; cout<<"language: "<<endl; Value &tmp=node3["language1"]; cout<<'\t'<<"language1: "<<tmp.GetString()<<endl; tmp=node3["language2"]; cout<<'\t'<<"language2: "<<tmp.GetString()<<endl; Value &node4=document["information"]; cout<<"information: "<<endl; if(node4.IsArray()) { int i=0; Value &data=node4[i]; //注意,此處下表索引只能用變量,不能用常量,例如node[0]編譯錯誤 cout<<'\t'<<"hobby: "<<data["hobby"].GetString()<<endl; i=1; data=node4[i]; cout<<'\t'<<"height: "<<data["height"].GetDouble()<<endl; } } int main(int argc,char **argv) { //寫、讀 測試 json_write(); json_read(); return 0; }