c++读写json,JsonCpp配置

JsonCpp配置

windows  VS编辑器;

下载 Jsoncpp 源码 , 链接 : https://github.com/open-source-parsers/jsoncpp

python amalgamate.py。运行amalgamate.py文件;

之后应该会报以下错误:

原因是因为 include文件下缺少version.h

创建version.h,内容如下:

 


// DO NOT EDIT. This file is generated by CMake from  "version" 
// and "version.h.in" files.
// Run CMake configure step to update it.
#ifndef JSON_VERSION_H_INCLUDED
# define JSON_VERSION_H_INCLUDED

# define JSONCPP_VERSION_STRING "0.6.0-dev"
# define JSONCPP_VERSION_MAJOR 0
# define JSONCPP_VERSION_MINOR 6
# define JSONCPP_VERSION_PATCH 0
# define JSONCPP_VERSION_QUALIFIER -dev
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))

#endif // JSON_VERSION_H_INCLUDED

之后运行amalgamate.py会有个dist文件夹;

dist下有json文件夹和jsoncpp.cpp文件,将这两个复制到自己的项目中,注意#include路径;

项目属性->VC++目录->包含目录 将json项目的include路径包含进去;

JsonCpp简单读写json文件

#include <iostream>
#include <fstream>
#include "json/json.h"


void write_json() {

	ofstream out;
	out.open("D:\\test.txt");

	Json::Value root;
	Json::StyledWriter write;
	root["name"] = "person";
	root["age"] = 18;

	out << write.write(root);
	out.close();
}

void read_json() {

	ifstream in;
	in.open("D:\\test.txt");

	Json::Reader read;
	Json::Value root;

	if(read.parse(in,root)) {
		Json::Value name = root["name"].asString();
		Json::Value age = root["age"].asInt();
		cout << root << endl;
		cout << name << endl;
		cout << age << endl;
	}

}


int main(int argc, char* argv[]) {
	write_json();
	read_json();

	system("pause");
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值