使用C++读取.json文件(利用cJSON.c和cJSON.h)

使用C++读取.json文件(利用cJSON.c和cJSON.h)

遇到问题了,谁来帮忙解决一下。。。万分感谢

读取配置文件 获取配置文件中的区域和时间信息,作为目标违停事件的判定条件。

1 config.json配置文件内容
{
	"camera_src":"1_1",
	"cameraFPS": 25,
	"describe": "Illegal Parking",
	"enable": true,
	"configRegion": [[15, 10],[65, 10],[62, 100],[18, 100]],
	"statisticTime(second)": 3, 
	"other":true
}
2 IllegalParking.h
struct Point
	{
		int x;
		int y;
	};
	struct ConfigInfos {
		std::vector<Point> region;
		int staFrameNum;
	};
	class EventIllegalParking {
	public:
		EventIllegalParking(){ };
		~EventIllegalParking(){ };

		const char* strPath;
		int getConfig(const char* strPath, ConfigInfos& configInfos);
		int getConfig111(const char* strPath, ConfigInfos& configInfos);
	private:
		int getFileSize(const char *strPath);
		char* readConfig(const char* strPath);
		}
3 IllegalParking.cpp
int EventIllegalParking::getFileSize(const char *strPath)
{
	if (NULL == strPath)
		return 0;
	struct stat filestat;
	memset(&filestat, 0, sizeof(struct stat));  // sizeof计算结构体占内存的大小
	/*获取文件信息*/
	if (0 == stat(strPath, &filestat))
		return filestat.st_size;  // 获取文件的字节数
	else
		return 0;
}

char* EventIllegalParking::readConfig(const char* strPath)
{
	int result_read = -1;

	FILE *fp = nullptr;
	fp = fopen(strPath, "rb");
	if (fp == nullptr) { return nullptr; }

	int size = getFileSize(strPath);

	/*malloc memory*/
	char *buf = (char*)malloc(size + 1);
	if (NULL == buf) 
	{
		printf("read_config() malloc failed!!!\n");
		return nullptr;
	}
	memset(buf, 0, size + 1);  // 读取内存位置用 0 填充
	fread(buf, 1, size, fp);
	fclose(fp);
	return buf;	
}



int EventIllegalParking::getConfig(const char* strPath, ConfigInfos& configInfos)
{
	// 同getConfig()功能,但取出来的数组值不知道为什么,总是有问题。
	// 读取配置文件信息
	char* text = readConfig(strPath);
	if (text == nullptr)  {  return 0;  }	
	cout << "text:" <<text<< endl;
	cJSON* text_json = cJSON_Parse(text);
	if (text_json == NULL) 
	{
		printf("the text_json file format is wrong: [%s]\n", cJSON_GetErrorPtr());
		return 0;
	}

	// 读取配置文件中的描述信息
	cJSON* describe = cJSON_GetObjectItem(text_json, "describe");     
	if (describe == nullptr)
	{
		cout << "describe == nullptr ==> return !" << endl;
		return 0;
	}
	if (describe->type == cJSON_String)
	{
		cout<< "describe: " << cJSON_Print(describe) << endl;
		//cout << "describe: " << describe->valuestring << endl;
	}
	

	// 读取配置文件中标定的区域信息
	cJSON* configRegion = cJSON_GetObjectItem(text_json, "configRegion"); 
	if (configRegion == nullptr)
	{
		cout << "configRegion == nullptr ==> return !" << endl;
		return 0;
	}

	int ptNum = cJSON_GetArraySize(configRegion);  // pt_num:eigen.rows
	if (ptNum < 3)
	{
		cout << "configRegion Number error !" << endl;
		return 0;
	}
	cout << "ptNum: " << ptNum << endl;
	for (int j = 0; j < ptNum; j++)
	{
		cJSON* sub_point = cJSON_GetArrayItem(configRegion, j); 
		// int subNum = cJSON_GetArraySize(sub_point);
		// cout << "subNum = "<< subNum << endl;          // 2
		// bool isAry = (sub_point->type==cJSON_Array);
		// cout<<"sub_point是否为数组  "<< isAry <<endl;   // 1		
		cJSON* pt_x = cJSON_GetArrayItem(sub_point, 0);
		cJSON* pt_y = cJSON_GetArrayItem(sub_point, 1);
		
		if (pt_x != nullptr && pt_y != nullptr)
		{
			Point pt;				
			cout << "cJSON_Print(pt_x) = " << cJSON_Print(pt_x) << endl;
			pt.x = (pt_x->valueint);   // cout<<cJSON_Print(pt_x)<<endl;
			pt.y = (pt_y->valueint);   // 不知道为什么,这里取出来的值并不是想要的值
	
			cout << "pt_x = " << pt_x << ", "<< "pt_y = " << pt_y << endl;	
			cout << "pt.x = " << pt.x << ", "<< "pt.y = " << pt.y << endl;	
			
			configInfos.region.push_back(pt);
		}
	}

    // 读取统计时间,换算成帧数
	cJSON* statisticTime = cJSON_GetObjectItem(text_json, "statisticTime(second)"); 
	cJSON* cameraFPS = cJSON_GetObjectItem(text_json, "cameraFPS"); 

	if (statisticTime == nullptr)
	{
		cout << "statisticTime == nullptr ==> return !" << endl;
		return 0;
	}
    
	if (cameraFPS->type )
	{
        cout<<"类型:"<<cameraFPS->type<< ";  value:" <<cJSON_Print(cameraFPS) <<endl;
	}


	cout << "cameraFPS->valueint = " << cameraFPS->valueint << endl;	
	configInfos.staFrameNum = (statisticTime->valueint)*(cameraFPS->valueint);
	cout << "configInfos.staFrameNum: " << configInfos.staFrameNum << endl;	
	
    free(text);

	return 1;
}
入口函数
int main()
{    
	EventIllegalParking* parking = new EventIllegalParking();
	
	const char* strPath = "../config.json";
	ConfigInfos configInfos;
	parking->getConfig(strPath, configInfos);
	return 0;
}
问题:

在这里插入图片描述

在这里插入图片描述

​ 如上图几个位置,打印结果如下图使用cJSON_Print(pt_x)打印出来的值是在正确的.json文件中的值,但是使用pt_x->valueint取出来的值不知道什么原因总是0,查了很多资料,感觉代码的写法上都是没有问题的,一直没找到原因,求助大神!!!

在这里插入图片描述

所有代码及链接

链接:https://pan.baidu.com/s/1MwIR2sFzerSn9oHgVttTrA 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃油淋鸡的莫何

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值