JSON格式配置文件

最近写了一个格式化打印订单的项目,用到配置,希望比较轻量,正好程序处理的数据是json格式的,于是顺便用libjsoncpp做了一个配置文件解析。


libjsoncpp的下载地址


而且整个项目可以直接以源文件方式添加到项目中, 跨平台做的比较不错。


Makefile

all:
	g++ -static -o ./bin/jsonformat -g -O0 -I./libjsoncpp/include -I./libjsoncpp/src jsonformat.cpp \
	libjsoncpp/src/json_reader.cpp \
	libjsoncpp/src/json_writer.cpp \
	libjsoncpp/src/json_value.cpp 
	


配置文件config.json

{
	"order":{
		"header": "好哥餐厅 微信订单"
	}, 
	"usbprint":{
		"printer": "Generic / Text Only"
	}
}

主程序部分代码

#include <json/json.h>

void GetOrderHeader(const std::string & configpath, std::string & header)
{
    std::ifstream myfile (configpath.c_str());
    Json::Value root; Json::Reader reader;
    if (reader.parse(myfile, root, false)){
        header = root["order"].get("header", "").asString();
    }
    return ;
}

int main(int argc, char * argv[])
{
    enum {MaxLineLen = 4000};
    std::string szHeader  = "";
    char szPath [MaxLineLen] = "config.json";
    GetOrderHeader(szPath, szHeader);  if (szHeader.empty())  return -2;
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值