23、C++解析json的使用(Rapidjson)

基本思想:提供一段json文件,进行解析及其转存关键信息

{"video_name":"xxx.mp4","
frame_rate":10,"video_frames":1004,"frame_width":1080,"frame_height":720,"camera
_id":"xxxxx","time":"3020-01-16 20:39:18.089","frame_total":1034,"frame_
results":[{"frame_num":0,"detect_results":[]},{"frame_num":2,"detect_results":[]
},{"frame_num":4,"detect_results":[]},{"frame_num":5,"detect_results":[]},{"fram
e_num":7,"detect_results":[]},{"frame_num":8,"detect_results":[]},{"frame_num":1
0,"detect_results":[]},{"frame_num":11,"detect_results":[]},{"frame_num":13,"det
ect_results":[]},{"frame_num":14,"detect_results":[]},{"frame_num":16,"detect_re
sults":[]},{"frame_num":19,"detect_results":[]},{"frame_num":20,"detect_results"
:[]},{"frame_num":22,"detect_results":[]},{"frame_num":23,"detect_results":[]},{
"frame_num":25,"detect_results":[]},{"frame_num":26,"detect_results":[]},{"frame
_num":28,"detect_results":[]},{"frame_num":29,"detect_results":[{"class_id":3,"class":"dog","score":0.6641687750
816345,"x_min":0.5097185373306274,"y_min":0.5797778367996216,"x_max":0.593885421
7529297,"y_max":0.7383118867874146},{"class_id":1,"class":"cat","score":0.49607
405066490173,"x_min":0.6755297183990479,"y_min":0,"x_max":0.9958200454711914,"y_
max":0.12107037752866745}]},{"frame_num":
31,"detect_results":[]}]}

a.json文件

进行文件解析和暂存

前提可以使用rapidjson

sudo apt-get install rapidjson-dev
#include<iostream>
#include<rapidjson/document.h>
#include <fstream>
#include <map>
#include <string>
#include<Eigen/Dense>
#include<vector>
using namespace std;
using namespace rapidjson;
using namespace Eigen;
typedef struct
{
	int class_id;
	string class_type;
	double score;
	double x_min;
	double y_min;
	double x_max;
	double y_max;

} INFO;



int main(int argc,char **argv)
{       
	INFO info;
	vector<INFO> vec_detect;
	map<int,vector<INFO> > map_detect;
	fstream file_in;
	Document document;
	const char* json_data="a.json";
	file_in.open(json_data);
	if (!file_in.is_open())
	{
		cout<<"open file is fail"<<endl;
	}
	string fileData( ( istream_iterator<char>(file_in) ),  istream_iterator<char>());
	if (!document.Parse(fileData.c_str()).HasParseError()) 
	{        
		if (document.HasMember("video_name") && document["video_name"].IsString())
			cout<<document["video_name"].GetString()<<endl;
		const Value& V=document["frame_results"];  
		for(int i = 0; i < V.Size(); ++i){
			cout<<V[i]["frame_num"].GetInt()<<endl;
			const Value& v=V[i]["detect_results"];

			for (int j=0;j<v.Size();j++)
			{ 
				info.class_id=v[j]["class_id"].GetDouble();
				info.class_type=v[j]["class"].GetString();
				info.score = v[j]["score"].GetDouble();
				info.x_min=v[j]["x_min"].GetDouble();
				info.y_min = v[j]["y_min"].GetDouble();
				info.x_max=v[j]["x_max"].GetDouble();
				info.y_max = v[j]["y_max"].GetDouble();
				vec_detect.push_back(info);
			}	
			map_detect[V[i]["frame_num"].GetInt()]=vec_detect;
		}   
	}
	//  存储json文件的内容 使用腾讯的rapidjson

	//test code


	 map<int ,vector<INFO> >::iterator iter= map_detect.find(29);
		if(iter != map_detect.end())
		    {
                   cout<<"Find, the value is"<<endl;
                    for(size_t i=0;i<((*iter).second).size();i++)
                      cout<<((*iter).second)[i].score<<endl;
}	
		else
			cout<<"Do not Find"<<endl;




	return 0;
}
ubuntu@ubuntu:~$ g++ main.cpp -o test
ubuntu@ubuntu:~$ ./test 
xxx.mp4
0
2
4
5
7
8
10
11
13
14
16
19
20
22
23
25
26
28
29
31
Find, the value is
0.664169
0.496074

 

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值