[json]C++读取json文件

4 篇文章 0 订阅

首先下载JsonCpp:链接:http://sourceforge.net/projects/jsoncpp/files/jsoncpp/

我下载的是0.6.0-rc2

下载后解压,并打开makefiles->vs71。原sln是7.1的。这里使用自己的当前编译器进行打开即可升级到当前版本

原文链接:https://blog.csdn.net/martinkeith/article/details/106869054

可能遇到的报错 “error LNK2019: 无法解析的外部符号”,我找了很久WIN32和WIN64冲突. 

编译和使用必须同时选择WIN32或WIN64 https://blog.csdn.net/woshigaowei5146/article/details/115656266

自己封装的接口 JsonConfig.h

#pragma once
#include <map>

#include "File.h"
#include "../libs/json/json.h"

using namespace std;

namespace JsonUtil
{
	int load_json_config(const char *jsonFile, Json::Value &con);

	int getInt(Json::Value &node, string fileName, int nDefault = 0);

	int getUInt(Json::Value &node, string fileName, int nDefault = 0);

	bool getBool(Json::Value &node, string fileName, bool  bDefault = false);

	string getString(Json::Value &node, string fileName, string  strDefault = "Error");

	double getDouble(Json::Value &node, string fileName, double  dDefault = 0);
}

JsonConfig.cpp

#include "JsonConfig.h"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace Extralib;

namespace JsonUtil
{
	int load_json_config(const char *doc, Json::Value &conf)
	{
		Json::Reader jsonReader;

		unsigned int size = 0;
		unsigned char *buffer = File::GetFileDataBuff(doc,size,1);

		if (buffer == 0)
		{
			printf("Failed to open file %s", doc);
			return -1;
		}

		std::string config_doc = (char*)buffer;
		delete[] buffer;

		if (jsonReader.parse(config_doc, conf) == false)
		{
			char szErrBuf[512] = {0};
			sprintf( szErrBuf, "解析Json文件错误%s error: %s\n",doc, jsonReader.getFormatedErrorMessages().c_str() );

			string strError = string(szErrBuf) + config_doc;
			if(strError.length() > 4000)
				strError = strError.substr(0,4000);

			printf(strError.c_str());
			return -1;
		}

		return 0;
	}
	
	int getInt(Json::Value &node, string fileName, int nDefault)
	{
		if(!node[fileName].isInt() && !node[fileName].isUInt())
			return nDefault;
		return node[fileName].asInt();
	}

	int getUInt(Json::Value &node, string fileName, int nDefault)
	{
		if(!node[fileName].isUInt() && !node[fileName].isInt())
			return nDefault;
		return node[fileName].asUInt();
	}

	bool gebool(Json::Value &node, string fileName, bool  bDefault)
	{
		if(!node[fileName].isBool() && !node[fileName].isInt())
			return bDefault;
		return node[fileName].asBool();
	}

	string getString(Json::Value &node, string fileName, string  strDefault)
	{
		if(!node[fileName].isString())
			return strDefault;
		return node[fileName].asString();		
	}

	double getDouble(Json::Value &node, string fileName, double  dDefault)
	{
		if(!node[fileName].isDouble() && !node[fileName].isInt() && !node[fileName].isUInt())
			return dDefault;
		return node[fileName].asDouble();
	}

}

测试代码配置 games.json

{
    "1": {
        "game_type": [
			"EN_Table_LZMJ",
			"EN_Table_FPF",
			"EN_Table_LZMJ_MATCH",
			"EN_Table_MJ_AB",
			"EN_Table_MJ_AY",
			"EN_Table_MJ_XZ",
			"EN_Table_MJ_XL",
			"EN_Table_MJ_JY",
			"EN_Table_MJ_DY",
			"EN_Table_MJ_NC",
			"EN_Table_MJ_NC_JD",
			"EN_Table_MJ_MY",
			"EN_Table_MJ_YB",
			"EN_Table_MJ_WZ",
			"EN_Table_MJ_ZG",
			"EN_Table_MJ_DZ",
			"EN_Table_MJ_NJ",
			"EN_Table_LZMJ_R",
			"EN_Table_MJ_XC",
			"EN_Table_MJ_LS",
			"EN_Table_MJ_DE_YANG",
			"EN_Table_MJ_ZI_YANG",
			"EN_Table_MJ_YA",
			"EN_Table_MJ_SN",
			"EN_Table_GZ_MJ_YH",
			"EN_Table_GZ_MJ_ZJ",
			"EN_Table_MJ_AQ_PF",
			"EN_Table_MJ_AQ_PF_2",
			"EN_Table_MJ_AQ",
			"EN_Table_MJ_XUAN_CHENG",
			"EN_Table_MJ_CZ",
			"EN_Table_MJ_GA",
			"EN_Table_LZ_MJ"
        ],
        "type": 1
    }
}

main.cpp

#include "JsonConfig.h"

Json::Value root;
if(JsonUtil::load_json_config( "../conf/games.json", root) != 0)
{
	printf("load game item config failed");
	return false;
}


for(Json::Value::iterator iter = root.begin(); iter != root.end(); ++iter)
{
	int key = atoi(iter.key().asCString());
	Json::Value &jsonValue = *iter;

	Json::Value& games = jsonValue["game_type"];
	for(Json::Value::iterator Itor = games.begin();Itor != games.end(); ++Itor)
	{
		Json::Value & Data = *Itor;
		string gameName = Data.asCString();
		//printf("%d, %s", key, gameName.c_str());
		const google::protobuf::EnumDescriptor *descriptor = ENTableType_descriptor();
		//string string_1 = descriptor->FindValueByNumber(1)->value();
		int gameId = descriptor->FindValueByName(gameName)->number();
		tableRecord::Instance()->InitCfg(gameId, key);
		//printf("%s, %d", gameName.c_str(), gameId);
	}
}

资源信息有json库

file.h头文件

【C++】文件读写

【json&&protobuf】把json字符串转protobuf枚举

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值