C++工程实践JsonCpp读取Json配置文件

C++工程实践JsonCpp读取Json配置文件

1 安装JsonCpp

vcpkg安装请参考链接 https://blog.csdn.net/qq_41023270/article/details/109274433

E:\CodeSamples\vcpkg>.\vcpkg install jsoncpp:x64-windows
Installing 3/3 jsoncpp:x64-windows…
Building jsoncpp[core]:x64-windows…
– Downloading https://github.com/open-source-parsers/jsoncpp/archive/1.9.5.tar.gz -> open-source-parsers-jsoncpp-1.9.5.tar.gz…
– Extracting source E:/CodeSamples/vcpkg/downloads/open-source-parsers-jsoncpp-1.9.5.tar.gz
– Using source at E:/CodeSamples/vcpkg/buildtrees/jsoncpp/src/1.9.5-13b47286ba.clean
– Found external ninja(‘1.10.2’).
– Configuring x64-windows
– Building x64-windows-dbg
– Building x64-windows-rel
CMake Warning at scripts/cmake/vcpkg_copy_pdbs.cmake:44 (message):
Could not find a matching pdb file for:

  • E:/CodeSamples/vcpkg/packages/jsoncpp_x64-windows/bin/jsoncpp.dll
    E:/CodeSamples/vcpkg/packages/jsoncpp_x64-windows/debug/bin/jsoncpp.dll

Call Stack (most recent call first):
ports/jsoncpp/portfile.cmake:30 (vcpkg_copy_pdbs)
scripts/ports.cmake:147 (include)
在这里插入图片描述

安装完成后, bin目录下为Release版本的 jsoncpp.dll, lib目录下为Release版本的 jsoncpp.lib, include为头文件, debug目录下为debug版本的dll和lib库.

2 使用JsonCpp读取json文件

hwinit.conf为json形式器件配置, 可以动态修改寄存器数值用于硬件调试,内容如下:

{
	"HwVersion": {
		"Version": 1.2,
		"Author": "test",
		"Date": "20220105",
		"Offcial":1
	},

	"ADF4002": {
		"DevId1": [{
				"addr": 1,
				"data": "0x1"
			},
			{
				"addr": 2,
				"data": "0x2"
			},
			{
				"addr": 3,
				"data": "0x3"
			}
		],
		"DevId2": [{
				"addr": 7,
				"data": "0x7"
			},
			{
				"addr": 8,
				"data": "0x8"
			},
			{
				"addr": 9,
				"data": "0x9"
			}
		]
	}

}

以下为读取上述数据代码:

// JsonSample.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <fstream>
#include "Windows.h"
#include "json/json.h"
#include "errno.h"
using namespace std;

class JsonSample
{
private:
	string  m_configFilename;
	Json::Value m_jsonConfigRoot;


public:
	explicit JsonSample(const string& configFilename)
		: m_configFilename(configFilename)
	{
		//std::cout << "Current path is " << m_configFilename << '\n'; 
	}

	int32_t InitConfig();
	void ReadAdf4001();
	void ReadHwVersion();

};

int32_t JsonSample::InitConfig()
{
	std::ifstream fileStream;
	fileStream.open(m_configFilename);
	if (!fileStream.is_open())
	{
		cout << "Config file open failed" << endl;
		return RETURN_ERROR;
	}

	Json::Reader JsonReader;
	if (!JsonReader.parse(fileStream, m_jsonConfigRoot))
	{
		cout << "Config file parse failed" << endl;
		return RETURN_ERROR;
	}
	
	return RETURN_OK;

}

void JsonSample::ReadAdf4001()
{
	int32_t ret = RETURN_OK;
	const uint32_t ADF_DEVICE_COUNT = 2;
	Json::Value adfJsonConf[ADF_DEVICE_COUNT];

	adfJsonConf[0] = m_jsonConfigRoot["ADF4002"]["DevId1"];
	adfJsonConf[1] = m_jsonConfigRoot["ADF4002"]["DevId2"];
	
	for (uint32_t i = 0; i < ADF_DEVICE_COUNT; ++i)
	{
		if (adfJsonConf[i].isArray())
		{
			for (uint32_t j = 0; j < adfJsonConf[i].size(); ++j)
			{
				int32_t addr = adfJsonConf[i][j]["addr"].asInt();
				string  data = adfJsonConf[i][j]["data"].asString();
				cout << "addr:" << addr << ",data:" << data << endl;
			}
		}
	}
	
	cout << endl;

}

void JsonSample::ReadHwVersion()
{
	Json::Value hardwareVersionJsonConf = m_jsonConfigRoot["HwVersion"];
	
	float version = hardwareVersionJsonConf["Version"].asFloat();
	string author = hardwareVersionJsonConf["Author"].asString();
	string date = hardwareVersionJsonConf["Date"].asString();
	bool official = hardwareVersionJsonConf["Offcial"].asBool();
	
	cout << "version:" << version << endl;
	cout << "author:" << author << endl;
	cout << "date:" << date << endl;
	cout << std::boolalpha  << "official:" << official << endl;
	
	cout << endl;

}

std::string GetCurrentDir()
{
	char buf[256] = { 0 };
	
	int length = GetModuleFileNameA(NULL, buf, sizeof(buf) - 1);
	if (length > 0)
	{
		for (int i = length - 1; i > 0; i--)
		{
			if (buf[i] == '\\')
			{
				buf[i] = '\0';
				break;
			}
		}
	}
	
	return buf;

}

int main()
{
	string currentDir = GetCurrentDir();
	JsonSample jsonSample(currentDir + "\\hwinit.conf");
    
	jsonSample.InitConfig();
	jsonSample.ReadHwVersion();
	jsonSample.ReadAdf4001();
	
	return 0;

}
Output:
c++version:1.2
author:test
date:20220105
official:true

addr:1,data:0x1
addr:2,data:0x2
addr:3,data:0x3
addr:7,data:0x7
addr:8,data:0x8
addr:9,data:0x9
源代码参考https://github.com/hero-joey/Cpp_Examples
要在C++中使用JsonCpp库来读取JSON文件,你可以按照以下步骤进行操作: 1. 首先,确保已经下载并安装了JsonCpp库,并将其包含在你的项目中。 2. 创建一个Json::Value对象来存储解析后的JSON数据。 3. 使用Json::Reader对象来读取JSON文件并解析为Json::Value对象。 下面是一个简单的示例代码,演示了如何使用JsonCpp读取JSON文件: ```cpp #include <iostream> #include <fstream> #include <json/json.h> int main() { // 读取 JSON 文件 std::ifstream jsonFile("example.json"); if (!jsonFile.is_open()) { std::cout << "Failed to open JSON file." << std::endl; return 1; } // 解析 JSON 数据 Json::Value root; Json::Reader reader; if (!reader.parse(jsonFile, root)) { std::cout << "Failed to parse JSON data." << std::endl; return 1; } // 读取 JSON 数据并输出 std::string name = root["name"].asString(); int age = root["age"].asInt(); std::string city = root["city"].asString(); std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "City: " << city << std::endl; return 0; } ``` 在上面的示例中,我们首先打开了一个名为`example.json`的JSON文件,然后使用JsonCppJson::Reader对象解析JSON数据,并将其存储在名为`root`的Json::Value对象中。接下来,我们通过访问`root`对象的键值对来获取JSON数据,并将其输出到控制台上。 请确保将示例代码中的`example.json`替换为你实际使用的JSON文件的路径。 这里提供一个CSDN上的相关教程供参考:[https://blog.csdn.net/qq_41453285/article/details/105808014](https://blog.csdn.net/qq_41453285/article/details/105808014)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值