使用C++来读取Json文件

本文介绍如何使用json.hpp从config_json.json文件中读取配置信息,并解析为C++类对象。涉及类包括CarType, EndTime等,涵盖车辆类型、违规行为等多种数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里有一个json的文件config_json.json。需要对其进行读取。利用json.hpp文件实现。

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include"json.hpp"
using namespace std;
using json = nlohmann::json;

class CarType {
public:
	int bus;
	int car;
	int microtruck;
	int truck;
};

class EndTime {
public:
	string time;
};

class FtpSet {
public:
	string dataBaseAddress;
	string passWord;
	string upFileDir;
	string userName;
};

class Illegal {
public:
	int antidromic;
	int bayonet;
	int car_occupy_ncar;
	int far_headlights;
	int green_light_stop;
	int illegal_change_lanes;
	int intersection_stop_car;
	int occupy_emergency_lane;
	int occupy_line;
	int rush_red_light;
	int thorugh_area;
	int use_phone;
	int violate_ban;
};

class ImageDataPass {
public:
	string imageDataIP;
	string imageDataName;
	string imagePassword;
	string recordDir;
};

class IntersectionInformation {
public:
	string car_Plate;
	string center_ip_address;
	string devicesID;
	string illegalCode;
	string intersectionName;
	string tpid;
};

class Param {
public:
	string gpu_State;
	string lawDriveWay;
	string runTimeYJ;
	string cut_back_Time;
	string cut_front_Time;
	string dispose_video;
};

class RoadSegment {
public:
	string segment;
};

class ServiceIP {
public:
	string serviceIP;
};

class ServicePort {
public:
	string servicePort;
};

class SqlData {
public:
	string dataBaseAddress;
	string passWord;
	string upFileDir;
	string userName;
};

int main()
{
	ifstream io;
	io.open("config_json.json");
	json j;
	io >> j;

	CarType cartype_data;
	EndTime time_data;
	FtpSet ftp_data;
	Illegal illegal_data;
	ImageDataPass image_data;
	IntersectionInformation intersectionInformation_data;
	Param param_data;
	RoadSegment roadSegment_data;
	ServiceIP serviceIP;
	ServicePort serviceport;
	SqlData sql_data;

	json json_cartype = j.operator[]("CarType").get<json>();
	cartype_data.bus = json_cartype.operator[]("BUS").get<int>();
	cartype_data.car = json_cartype.operator[]("CAR").get<int>();
	cartype_data.microtruck = json_cartype.operator[]("MICROTRUCK").get<int>();
	cartype_data.truck = json_cartype.operator[]("TRUCK").get<int>();
/*************************************************************************************/	

	json json_time = j.operator[]("EndTime").get<json>();
	time_data.time = json_time.get<string>();

/**************************************************************************************/
	
	json json_ftpset = j.operator[]("FtpSet").get<json>();
	ftp_data.dataBaseAddress = json_ftpset.operator[]("DataBaseAddress").get<string>();
	ftp_data.passWord = json_ftpset.operator[]("PassWord").get<string>();
	ftp_data.upFileDir = json_ftpset.operator[]("UpFileDir").get<string>();
	ftp_data.userName = json_ftpset.operator[]("UserName").get<string>();

/**************************************************************************************/

	json json_illegal = j.operator[]("Illegal_information").get<json>();
	illegal_data.antidromic = json_illegal.operator[]("antidromic").get<int>();
	illegal_data.bayonet = json_illegal.operator[]("bayonet").get<int>();
	illegal_data.car_occupy_ncar = json_illegal.operator[]("car_occupy_ncar").get<int>();
	illegal_data.far_headlights = json_illegal.operator[]("far_headlights").get<int>();
	illegal_data.green_light_stop = json_illegal.operator[]("green_light_stop").get<int>();
	illegal_data.illegal_change_lanes = json_illegal.operator[]("illegal_change_lanes").get<int>();
	illegal_data.intersection_stop_car = json_illegal.operator[]("intersection_stop_car").get<int>();
	illegal_data.occupy_emergency_lane = json_illegal.operator[]("occupy_emergency_lane").get<int>();
	illegal_data.occupy_line = json_illegal.operator[]("occupy_line").get<int>();
	illegal_data.rush_red_light = json_illegal.operator[]("rush_red_light").get<int>();
	illegal_data.thorugh_area = json_illegal.operator[]("thorugh_area").get<int>();
	illegal_data.use_phone = json_illegal.operator[]("use_phone").get<int>();
	illegal_data.violate_ban = json_illegal.operator[]("violate_ban").get<int>();

/**************************************************************************************/

	json json_imagedatapass = j.operator[]("ImageDataPass").get<json>();
	image_data.imageDataIP = json_imagedatapass.operator[]("ImageDataIP").get<string>();
	image_data.imageDataName = json_imagedatapass.operator[]("ImageDataName").get<string>();
	image_data.imagePassword = json_imagedatapass.operator[]("ImagePassword").get<string>();
	image_data.recordDir = json_imagedatapass.operator[]("RecordDir").get<string>();

/**************************************************************************************/

	json json_intersectioninformation = j.operator[]("IntersectionInformation").get<json>();
	intersectionInformation_data.car_Plate = json_intersectioninformation.operator[]("Car_Plate").get<string>();
	intersectionInformation_data.center_ip_address = json_intersectioninformation.operator[]("Center_ip_address").get<string>();
	intersectionInformation_data.devicesID = json_intersectioninformation.operator[]("DevicesID").get<string>();
	intersectionInformation_data.illegalCode = json_intersectioninformation.operator[]("IllegalCode").get<string>();
	intersectionInformation_data.intersectionName = json_intersectioninformation.operator[]("IntersectionName").get<string>();
	intersectionInformation_data.tpid = json_intersectioninformation.operator[]("Tpid").get<string>();

/**************************************************************************************/

	json json_param = j.operator[]("Param").get<json>();
	param_data.gpu_State = json_param.operator[]("Gpu_State").get<string>();
	param_data.lawDriveWay = json_param.operator[]("LawDriveWay").get<string>();
	param_data.runTimeYJ = json_param.operator[]("RunTimeYJ").get<string>();
	param_data.cut_back_Time = json_param.operator[]("cut_back_Time").get<string>();
	param_data.cut_front_Time = json_param.operator[]("cut_front_Time").get<string>();
	param_data.dispose_video = json_param.operator[]("dispose_video").get<string>();
/**************************************************************************************/

	json json_roadsegment = j.operator[]("RoadSegment").get<json>();
	roadSegment_data.segment = json_roadsegment.get<string>();

	json json_serviceip = j.operator[]("ServiceIP").get<json>();
	serviceIP.serviceIP = json_serviceip.get<string>();

	json json_serviceport = j.operator[]("ServicePort").get<json>();
	serviceport.servicePort = json_serviceport.get<string>();

/**************************************************************************************/

	json json_sqldata = j.operator[]("SqlData").get<json>();
	sql_data.dataBaseAddress = json_sqldata.operator[]("DataBaseAddress").get<string>();
	sql_data.passWord = json_sqldata.operator[]("PassWord").get<string>();
	sql_data.upFileDir = json_sqldata.operator[]("UpFileDir").get<string>();
	sql_data.userName = json_sqldata.operator[]("UserName").get<string>();

/**************************************************************************************/

/**************************************************************************************/
	cout << cartype_data.bus << ","
		 << cartype_data.car << ","
		 << cartype_data.microtruck << ","
		 << cartype_data.truck << endl;

	cout << time_data.time << endl;

	cout << ftp_data.dataBaseAddress << ","
		 << ftp_data.passWord << ","
		 << ftp_data.upFileDir << ","
		 << ftp_data.userName << endl;

	cout << illegal_data.antidromic << ","
		 << illegal_data.bayonet << ","
		 << illegal_data.car_occupy_ncar << ","
		 << illegal_data.far_headlights << ","
		 << illegal_data.green_light_stop << ","
		 << illegal_data.illegal_change_lanes << ","
		 << illegal_data.intersection_stop_car<< ","
		 << illegal_data.occupy_emergency_lane << ","
		 << illegal_data.occupy_line << ","
		 << illegal_data.rush_red_light << ","
		 << illegal_data.thorugh_area << ","
		 << illegal_data.use_phone << ","
		 << illegal_data.violate_ban << endl;

	cout << image_data.imageDataIP << ","
		 << image_data.imageDataName << ","
		 << image_data.imagePassword << ","
		 << image_data.recordDir << endl;

	cout << intersectionInformation_data.car_Plate << ","
		 << intersectionInformation_data.center_ip_address << ","
		 << intersectionInformation_data.devicesID << ","
		 << intersectionInformation_data.illegalCode << ","
		 << intersectionInformation_data.intersectionName << ","
		 << intersectionInformation_data.tpid << endl;

	cout << param_data.gpu_State << ","
		 << param_data.lawDriveWay << ","
		 << param_data.runTimeYJ << ","
		 << param_data.cut_back_Time << ","
		 << param_data.cut_front_Time << ","
		 << param_data.dispose_video << endl;

	cout << roadSegment_data.segment << "\n" << serviceIP.serviceIP << "\n" << serviceport.servicePort << endl;

	cout << sql_data.dataBaseAddress << ","
		 <<sql_data.passWord << ","
		 << sql_data.upFileDir << ","
		 << sql_data.userName << endl;

	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值