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");
}