一、json库配置安装
https://blog.csdn.net/qq844352155/article/details/46945157
https://blog.csdn.net/qq_30236169/article/details/92829710
一 编译链接
1 在相应官网下载jsoncpp
2 解压得到jsoncpp-src-0.5.0文件
3 打开jsoncpp-src-0.5.0 -> makefiles -> vs71 -> jsoncpp.sln
4 转换项目为VS2010格式
5 选择debug模式
6 在“解决方案资源管理器”中右击 lib_json 选择->仅用于项目 -> 仅生成lib_json
7 再次右击 lib_json 选择->仅用于项目 -> 仅链接lib_json
8 选择release模式
9 重复操作 6 和 7
10 生成的.lib文件夹在jsoncpp-src-0.5.0 ->build -> vs71 中。
二 创建静态链接库SDK
1 在 D:\study\toolsForVS2010 (这里换成你自己想要放置静态链接库SDK的位置)中创建文件夹 jsonPP
2 在jsonPP中创建文件夹 include 和 lib
3 在include中创建文件夹 json ,将jsoncpp-src-0.5.0 -> include -> json 中的.h文件全部复制过来
4 在lib中创建文件夹debug和release ,把jsoncpp-src-0.5.0 ->build -> vs71 -> debug -> lib_json 中的.lib文件(比如json_vc71_libmtd.lib)复制到debug文件夹(此时可以修改文件夹名为json.lib),同理,把jsoncpp-src-0.5.0 ->build -> vs71 -> release -> lib_json 中的.lib文件(比如json_vc71_libmtd.lib)复制到release文件夹
三 打开要添加json的项目,或新建一个WIN32控制台程序(添加main.cpp,加入源代码,示例代码可在jsoncpp.sln的 json_test中找到)
1 选择 项目 ->属性
2 选择 配置属性 -> C/C++ -> 常规 右边的“附加包含目录” 添加include文件夹 :D:\study\toolsForVS2010\jsonPP\include
3 选择 配置属性 -> C/C++ -> 代码生成 右边的“运行库” 选择 MTd
4 选择 配置属性 -> 连接器 -> 常规 右边的“附加库目录” 添加lib文件夹 :D:\study\toolsForVS2010\jsonPP\lib\debug
5 选择 配置属性 -> 连接器 -> 输入 右边的“附加依赖项” 添加lib文件 :json_vc71_libmtd.lib(若在 二 - 4 中修改了文件名为json.lib,则此处改成json.lib)
6 点击应用完成json配置。
7 若项目需要发布时(即项目选择release),则
a. 选择 配置属性 -> C/C++ -> 代码生成 右边的“运行库” 选择MT
b. 选择 配置属性 -> 连接器 -> 常规 右边的“附加库目录” 添加lib文件夹 :D:\study\toolsForVS2010\jsonPP\lib\release
c. 然后可以运行程序获得release版本程序
二、c++ json文件的读写
#include <json.h>
#include <fstream>
#include <iostream>
#include <string>
#include <memory>
#include <sstream>
using namespace std;
void fillStuff(Json::Value& root)
{
Json::Value performances, performance;
root["customer"] = "BigCo";
performance["playID"] = "hamlet";
performance["audience"] = 55;
performances[0] = performance;
performance["playID"] = "as-like";
performance["audience"] = 35;
performances[1] = performance;
performance["playID"] = "othello";
performance["audience"] = 40;
performances[2] = performance;
root["performances"] = performances;
}
void createJson1()
{
cout << "========" << "creat json invoices.json" << "========" << endl;
Json::Value root;
fillStuff(root); // 填写JSON内容
// 将JSON内容(缩进格式)输出到文件
ofstream os;
os.open("invoices.json");
Json::StreamWriterBuilder writerBuilder;
std::unique_ptr<Json::StreamWriter> writer(writerBuilder.newStreamWriter());
writer->write(root, &os); //从writer中读取并存到文件流
os.close();
// 将JSON内容(缩进格式)显示到终端
writer->write(root, &cout); //从writer中读取并存到输出流
cout << endl;
}
void createJson2()
{
cout << "========" << "creat json invoices.json" << "========" << endl;
Json::Value root;
fillStuff(root); // 填写JSON内容
// 将JSON内容(缩进格式)输出到文件
ofstream os;
os.open("invoices.json");
os << root.toStyledString(); //转换为json格式并存到文件流
os.close();
}
void printJson(Json::Value& root)
{
cout << "customer: " << root["customer"].asString() << endl;
cout << "performances:" << endl;
for (int i = 0; i < 3; i++)
{
cout << " {" << "playID:" << root["performances"][i]["playID"].asString() << "; ";
cout << " " << "audience:" << root["performances"][i]["audience"].asInt() << "}," << endl;
}
}
bool parseJson1()
{// 直接解析文件流
cout << "========" << "read json invoices.json" << "========" << endl;
// 以二进制形式读取json文件内容
ifstream is("invoices.json", ios::binary);
if (!is.is_open())
{
cout << "open json file failed." << endl;
return false;
}
bool res;
JSONCPP_STRING errs;
Json::Value root;
Json::CharReaderBuilder readerBuilder;
res = Json::parseFromStream(readerBuilder, is, &root, &errs);
if (!res || !errs.empty()) {
cout << "parseJson err. " << errs << endl;
is.close();
return false;
}
printJson(root);
is.close();
return true;
}
bool parseJson2()
{ // 通过CharReader解析string
// 以二进制形式读取json文件内容
ifstream is("invoices.json", ios::binary);
if (!is.is_open())
{
cout << "open json file failed." << endl;
return false;
}
// 转换成string
ostringstream os;
os << is.rdbuf();
string info = os.str();
os.clear();
os.str("");
if (info.empty())
return false;
bool res;
JSONCPP_STRING errs;
Json::Value root;
Json::CharReaderBuilder crbuilder;
std::unique_ptr<Json::CharReader> const jsonReader(crbuilder.newCharReader());
res = jsonReader->parse(info.c_str(), info.c_str() + info.length(), &root, &errs);
if (!res || !errs.empty()) {
cout << "parseJson err. " << errs << endl;
return false;
}
printJson(root);
is.close();
return true;
}
int main()
{
createJson1();
// createJson2();
parseJson1();
// parseJson2();
return 0;
}
最后输出打印: