转载:
https://blog.csdn.net/qq849635649/article/details/52678822
#include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" #include <iostream> #include <string> using namespace std; void Serialize_1() { rapidjson::StringBuffer strBuf; rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf); writer.StartObject(); //1. 整数类型 writer.Key("Int"); writer.Int(1); //2. 浮点类型 writer.Key("Double"); writer.Double(12.0000001); //3. 字符串类型 writer.Key("String"); writer.String("This is a string"); //4. 结构体类型 writer.Key("Object"); writer.StartObject(); writer.Key("name"); writer.String("qq849635649"); writer.Key("age"); writer.Int(25); writer.EndObject(); //5. 数组类型 //5.1 整型数组 writer.Key("IntArray"); writer.StartArray(); //顺序写入即可 writer.Int(10); writer.Int(20); writer.Int(30); writer.EndArray(); //5.2 浮点型数组 writer.Key("DoubleArray"); writer.StartArray(); for(int i = 1; i < 4; i++) { writer.Double(i * 1.0); } writer.EndArray(); //5.3 字符串数组 writer.Key("StringArray"); writer.StartArray(); writer.String("one"); writer.String("two"); writer.String("three"); writer.EndArray(); //5.4 混合型数组 //这说明了,一个json数组内容是不限制类型的 writer.Key("MixedArray"); writer.StartArray(); writer.String("one"); writer.Int(50); writer.Bool(false); writer.Double(12.005); writer.EndArray(); //5.5 结构体数组 writer.Key("People"); writer.StartArray(); for(int i = 0; i < 3; i++) { writer.StartObject(); writer.Key("name"); writer.String("qq849635649"); writer.Key("age"); writer.Int(i * 10); writer.Key("sex"); writer.Bool((i % 2) == 0); writer.EndObject(); } writer.EndArray(); writer.EndObject(); string data = strBuf.GetString(); cout << data << endl; } --------------------- 作者:奋斗-少年 来源:CSDN 原文:https://blog.csdn.net/qq849635649/article/details/52678822 版权声明:本文为博主原创文章,转载请附上博文链接!
解析:
void parse() { //创建解析对象 rapidjson::Document doc; //首先进行解析,没有解析错误才能进行具体字段的解析 if(!doc.Parse(data.data()).HasParseError()) { //1. 解析整数 if(doc.HasMember("Int") && doc["Int"].IsInt()) { cout << "Int = " << doc["Int"].GetInt() << endl; } //2. 解析浮点型 if(doc.HasMember("Double") && doc["Double"].IsDouble()) { cout << "Double = " << doc["Double"].GetDouble() << endl; } //3. 解析字符串 if(doc.HasMember("String") && doc["String"].IsString()) { cout << "String = " << doc["String"].GetString() << endl; } //4. 解析结构体 if(doc.HasMember("Object") && doc["Object"].IsObject()) { const rapidjson::Value& object = doc["Object"]; if(object.HasMember("name") && object["name"].IsString()) { cout << "Object.name = " << object["name"].GetString() << endl; } if(object.HasMember("age") && object["age"].IsInt()) { cout << "Object.age = " << object["age"].GetInt() << endl; } } //5. 解析数组类型 //5.1 整型数组类型 if(doc.HasMember("IntArray") && doc["IntArray"].IsArray()) { //5.1.1 将字段转换成为rapidjson::Value类型 const rapidjson::Value& array = doc["IntArray"]; //5.1.2 获取数组长度 size_t len = array.Size(); //5.1.3 根据下标遍历,注意将元素转换为相应类型,即需要调用GetInt() for(size_t i = 0; i < len; i++) { cout << "IntArray[" << i << "] = " << array[i].GetInt() << endl; } } //5.2 浮点型数组类型 if(doc.HasMember("DoubleArray") && doc["DoubleArray"].IsArray()) { const rapidjson::Value& array = doc["DoubleArray"]; size_t len = array.Size(); for(size_t i = 0; i < len; i++) { //为防止类型不匹配,一般会添加类型校验 if(array[i].IsDouble()) { cout << "DoubleArray[" << i << "] = " << array[i].GetDouble() << endl; } } } //5.3 字符串数组类型 if(doc.HasMember("StringArray") && doc["StringArray"].IsArray()) { const rapidjson::Value& array = doc["StringArray"]; size_t len = array.Size(); for(size_t i = 0; i < len; i++) { //为防止类型不匹配,一般会添加类型校验 if(array[i].IsString()) { cout << "StringArray[" << i << "] = " << array[i].GetString() << endl; } } } //5.4 混合型 if(doc.HasMember("MixedArray") && doc["MixedArray"].IsArray()) { const rapidjson::Value& array = doc["MixedArray"]; size_t len = array.Size(); for(size_t i = 0; i < len; i++) { //为防止类型不匹配,一般会添加类型校验 if(array[i].IsString()) { cout << "MixedArray[" << i << "] = " << array[i].GetString() << endl; } else if(array[i].IsBool()) { cout << "MixedArray[" << i << "] = " << array[i].GetBool() << endl; } else if(array[i].IsInt()) { cout << "MixedArray[" << i << "] = " << array[i].GetInt() << endl; } else if(array[i].IsDouble()) { cout << "MixedArray[" << i << "] = " << array[i].GetDouble() << endl; } } } //5.5 结构体数组类型 if(doc.HasMember("People") && doc["People"].IsArray()) { const rapidjson::Value& array = doc["People"]; size_t len = array.Size(); for(size_t i = 0; i < len; i++) { const rapidjson::Value& object = array[i]; //为防止类型不匹配,一般会添加类型校验 if(object.IsObject()) { cout << "ObjectArray[" << i << "]: "; if(object.HasMember("name") && object["name"].IsString()) { cout << "name=" << object["name"].GetString(); } if(object.HasMember("age") && object["age"].IsInt()) { cout << ", age=" << object["age"].GetInt(); } if(object.HasMember("sex") && object["sex"].IsBool()) { cout << ", sex=" << (object["sex"].GetBool() ? "男" : "女") << endl; } } } } } /** * 最后注意:因为rapidjson不会做安全校验,所以要自己做安全校验,以int整型为例 * “if(object.HasMember("age") && object["age"].IsInt()) {}” * 这句校验很重要,既要校验有该子段,也要校验类型正确,否则会引发程序崩溃 */ } --------------------- 作者:奋斗-少年 来源:CSDN 原文:https://blog.csdn.net/qq849635649/article/details/52678822 版权声明:本文为博主原创文章,转载请附上博文链接!
顺便贴一下我自己的使用:
int Alg_VIR_application::saveAdnTransWhiteBoxTestInfo(sApplicationInput &applicationInput, sWhiteBoxTestInfo &whiteBoxTestInfo, std::string jsonTestInfo) { #ifdef SAVE_WHITE_BOX_TEST_INFO if (!applicationInput.paramWhiteBoxTestOpenFlag) return PROCESS_SUCCESS; std::vector<cv::Point> vecPtShift = applicationInput.vecPtShift; //将公共的信息从applicationInput中获取并塞入至whiteBoxTestInfo中 whiteBoxTestInfo.recPlateNum = applicationInput.regPlateNum; whiteBoxTestInfo.objLocation = applicationInput.car_location_res.objLocation; whiteBoxTestInfo.vvPoliceManDetectionOutput = applicationInput.vvPoliceManDetectionOutput; int CountobjLoc = MIN(whiteBoxTestInfo.objLocation.size(), vecPtShift.size()); int CountPoliceManDetection = MIN(whiteBoxTestInfo.vvPoliceManDetectionOutput.size(), vecPtShift.size()); int CountTrifficLight = MIN(whiteBoxTestInfo.ARLTestInfo.TrifficLight.objects.size(), vecPtShift.size()); //与违法类型相关的信息,在前面进行app内进行处理的时候已经塞值进去了,因此现在whiteBoxTestInfo中已经是全的值,可以进行json的转换了 rapidjson::StringBuffer strBuf; rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf); writer.StartObject(); //4. 结构体类型 writer.Key("recPlateNum"); writer.String(whiteBoxTestInfo.recPlateNum.c_str()); //5.5 结构体数组,写目标车辆的位置 writer.Key("vvObjectLoc"); writer.StartObject(); for(int j = 0; j < CountobjLoc; j++) { writer.Key(("objectLocation" + std::to_string(j)).c_str()); writer.StartArray(); writer.Int(whiteBoxTestInfo.objLocation[j].x + vecPtShift[j].x); writer.Int(whiteBoxTestInfo.objLocation[j].y + vecPtShift[j].y); writer.Int(whiteBoxTestInfo.objLocation[j].width); writer.Int(whiteBoxTestInfo.objLocation[j].height); writer.EndArray(); } writer.EndObject(); //5.5 结构体数组,写交警的位置 writer.Key("vvPoliceManDetectionOutput"); writer.StartObject(); for(int i = 0; i < CountPoliceManDetection; i++) { writer.Key(("PoliceManDetection" + std::to_string(i)).c_str()); writer.StartArray(); for(int j = 0; j < whiteBoxTestInfo.vvPoliceManDetectionOutput[i].size(); j++) { if ( whiteBoxTestInfo.vvPoliceManDetectionOutput[i][j].policeStatus == PCLPolice) { writer.Int(whiteBoxTestInfo.vvPoliceManDetectionOutput[i][j].pedestrianLoc.x + vecPtShift[i].x); writer.Int(whiteBoxTestInfo.vvPoliceManDetectionOutput[i][j].pedestrianLoc.y + vecPtShift[i].y); writer.Int(whiteBoxTestInfo.vvPoliceManDetectionOutput[i][j].pedestrianLoc.width); writer.Int(whiteBoxTestInfo.vvPoliceManDetectionOutput[i][j].pedestrianLoc.height); } } writer.EndArray(); } writer.EndObject(); //写红绿灯返回的结构体 writer.Key("sARLTestInfo"); writer.StartObject(); { writer.Key("SingleTrifficLightState"); writer.StartObject(); if (whiteBoxTestInfo.ARLTestInfo.TrifficLight.objects.size() > 0) { for(int i = 0; i < CountTrifficLight; i++) { writer.Key(("SingleTrifficLightState" + std::to_string(i)).c_str()); writer.StartArray(); for(int j = 0; j < whiteBoxTestInfo.ARLTestInfo.TrifficLight.objects[i].size(); j++) { writer.Int(whiteBoxTestInfo.ARLTestInfo.TrifficLight.objects[i][j].lightRect.x + vecPtShift[i].x); writer.Int(whiteBoxTestInfo.ARLTestInfo.TrifficLight.objects[i][j].lightRect.y + vecPtShift[i].y); writer.Int(whiteBoxTestInfo.ARLTestInfo.TrifficLight.objects[i][j].lightRect.width); writer.Int(whiteBoxTestInfo.ARLTestInfo.TrifficLight.objects[i][j].lightRect.height); writer.Int(whiteBoxTestInfo.ARLTestInfo.TrifficLight.objects[i][j].lightClass); } writer.EndArray(); } } writer.EndObject(); { writer.Key("WholeTrifficLightState"); writer.StartArray(); writer.Int(whiteBoxTestInfo.ARLTestInfo.TrifficLight.trafficLightState.sLeftState); writer.Int(whiteBoxTestInfo.ARLTestInfo.TrifficLight.trafficLightState.sStraightState); writer.Int(whiteBoxTestInfo.ARLTestInfo.TrifficLight.trafficLightState.sRightState); writer.EndArray(); } } writer.Key("LaneDircetion"); writer.Int(whiteBoxTestInfo.ARLTestInfo.LaneDircetion); writer.Key("CarDirection"); writer.Int(whiteBoxTestInfo.ARLTestInfo.CarDirection); writer.EndObject(); //写逆行返回的结构体 writer.Key("sRRTestInfo"); writer.StartObject(); writer.Key("LaneDircetion"); writer.Int(whiteBoxTestInfo.RRTestInfo.LaneDircetion); writer.Key("CarPose"); writer.Int(whiteBoxTestInfo.RRTestInfo.CarPose); writer.Key("CarDirection"); writer.Int(whiteBoxTestInfo.RRTestInfo.CarDirection); writer.Key("reverseLaneProportion"); writer.Double(whiteBoxTestInfo.RRTestInfo.reverseLaneProportion); writer.Key("overStopLineProportion"); writer.Double(whiteBoxTestInfo.RRTestInfo.overStopLineProportion); writer.EndObject(); //写违反导向返回的结构体 writer.Key("sADTestInfo"); writer.StartObject(); writer.Key("LaneDircetion"); writer.Int(whiteBoxTestInfo.RRTestInfo.LaneDircetion); writer.Key("CarPose"); writer.Int(whiteBoxTestInfo.RRTestInfo.CarPose); writer.EndObject(); //写压线返回的结构体 writer.Key("sCLALTestInfo"); writer.StartObject(); writer.Key("CarBodyRate"); writer.Double(whiteBoxTestInfo.CLALTestInfo.CarBodyRate); writer.EndObject(); writer.EndObject(); string json = strBuf.GetString(); std::ofstream fout; std::string root_path = "/media/tmp/test/"; fout.open(root_path + "11111.json"); fout << json << std::endl; fout.close(); #endif }
生成的json如下:
{ "recPlateNum":"浙G7FQ66", "vvObjectLoc":{ "objectLocation0":[ 362, 641, 272, 252 ], "objectLocation1":[ 2178, 475, 158, 152 ], "objectLocation2":[ 146, 1340, 185, 73 ] }, "vvPoliceManDetectionOutput":{ "PoliceManDetection0":[ 1114, 148, 18, 42, 1156, 148, 17, 40 ], "PoliceManDetection1":[ 2813, 148, 17, 41, 2716, 139, 23, 34, 2850, 154, 19, 41 ], "PoliceManDetection2":[ 1119, 1185, 18, 38, 1188, 1201, 19, 38 ] }, "sARLTestInfo":{ "SingleTrifficLightState":{ "SingleTrifficLightState0":[ 633, 22, 38, 11, 2, 600, 22, 32, 12, 3 ], "SingleTrifficLightState1":[ 2329, 22, 37, 12, 2, 2296, 21, 29, 13, 3 ], "SingleTrifficLightState2":[ 633, 1057, 38, 12, 2, 598, 1057, 33, 13, 3 ] }, "WholeTrifficLightState":[ 1, 1, 3 ], "LaneDircetion":99, "CarDirection":3 }, "sRRTestInfo":{ "LaneDircetion":99, "CarPose":99, "CarDirection":9, "reverseLaneProportion":0, "overStopLineProportion":0 }, "sADTestInfo":{ "LaneDircetion":99, "CarPose":99 }, "sCLALTestInfo":{ "CarBodyRate":0 } }