json在visual studio 2015上面的编译以及使用

由于项目需求,需要将数据存储为json格式,于是花了点时间编译json的源码;网上的编译json之后,调用API的方法会出如下错误 :“error C4996: 'Json::Reader': Use CharReader and CharReaderBuilder instead”;接下来本人会贴出编译和调用jsoncpp的方法。

一、编译json

先在github上下载源码,链接:https://github.com/open-source-parsers/jsoncpp,打开makefiles\msvc2010\jsoncpp.sln,编译成功后生成lib_json.lib文件

二、配置json

将jsoncpp-master/include/json配置到编译器的包含目录,再将lib_json.lib所在目录配置到库目录

三、在vs2015中使用json

#include <string>  
#include <iostream>  
#include <json.h>
#include <fstream>
#include <memory>

#pragma comment(lib, "lib_json")  

void testjson(std::map<std::string, std::string> mydata)
{
	Json::Value root;
	Json::StreamWriterBuilder  builder;
	builder["commentStyle"] = "None";
	builder["indentation"] = "";
	std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());

	Json::Value data;
	std::map<std::string, std::string>::iterator iter;
	for (iter = mydata.begin(); iter != mydata.end(); iter++)
	{
		data[iter->first] = iter->second;
	}
	root.append(data);

	std::ofstream ofs;
	ofs.open("result.json", std::ios::app);
	writer->write(root, &ofs);
}

void main(int argc, char** argv)
{
	std::map<std::string, std::string> mapMainInfo;
	mapMainInfo.insert(std::pair<std::string, std::string>("country", "chinese"));
	testjson(mapMainInfo);
	system("pause");
}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值