JsonCpp使用方法及示例

文章目录

    • 下载
    • 使用静态库/动态库
    • 使用源文件
    • demo
      • 头文件
      • 创建
      • 保存
      • 读取
      • 修改
      • main

下载

Github地址

本人编译好的库以及生产的源码

使用静态库/动态库

linux:

mkdir  build
cd build
cmake -DCMAKE_BUILD_TYPE=release -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=ON -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ..
make

windows:
下载cmake 编译

使用源文件

在项目目录中直接执行

python amalgamate.py

会在dist目录下生成两个头文件和一个源文件json-forwards.h 、json.h和jsoncpp.cpp;
直接放到工程中其他代码一起编译即可。

demo

头文件

#include 
#include "json/json.h"
#include 

创建

std::string MPT_test_createJson(void)
{
	std::string jsonStr;
	Json::Value root, fruit, mail;
	Json::StreamWriterBuilder writerBuilder;
	std::ostringstream os;
	//自动换行
	writerBuilder.settings_["indentation"] = "\t";

	root["Name"] = "Liangbaikai";
	root["Age"] = 25;
	root["gender"] = "man";

	fruit[0] = "apple";
	fruit[1] = "orange";
	fruit[2] = "banana";
	root["fruit"] = fruit;

	mail["QQ"] = "[email protected]";
	mail["Google"] = "[email protected]";
	root["mail"] = mail;

	// jsonWriter是智能指针, 当jsonWriter被析构时, 它所指向的对象(内存)也被自动释放
	std::unique_ptr jsonWriter(writerBuilder.newStreamWriter());
	jsonWriter->write(root, &os);
	jsonStr = os.str();

	std::cout << "Json:" << jsonStr << std::endl;
	return jsonStr;
}

保存

void MPT_test_saveJson(std::string file, std::string& jsonStr)
{
	std::ofstream ofs;
	ofs.open(file);
	if (!ofs.is_open())
	{
		std::cout << "open json file failed." << std::endl;
		return;
	}
	ofs << jsonStr;
	ofs.close();
	return;
}

读取

void MPT_test_readJson(std::string file) {
	std::ifstream ifs;
	ifs.open(file, std::ios::in);
	if (!ifs.is_open())
	{
		std::cout << "open json file failed." << std::endl;
		return;
	}

	Json::CharReaderBuilder reader;
	Json::Value root, fruit, mail;

	int iage = 0;
	std::string sname;
	JSONCPP_STRING errs;

	bool res = Json::parseFromStream(reader, ifs, &root, &errs);
	if (!res || !errs.empty())
	{
		std::cout << "parseJson error! " << errs << std::endl;
		return;
	}

	iage = root["Age"].asInt();
	sname = root["Name"].asString();
	fruit = root["fruit"];
	mail = root["mail"];
	ifs.close();

	std::cout << "age: " << iage << std::endl;
	std::cout << "name: " << sname << std::endl;
	std::cout << "fruit: " << fruit[0] << fruit[1] << fruit[2] << std::endl;
	std::cout << "mailqq:" << mail["QQ"] << std::endl;
	return;
}

修改

void MPT_test_changeJson(std::string file) {
	std::ifstream ifs;
	ifs.open(file, std::ios::in);
	if (!ifs.is_open())
	{
		std::cout << "open json file failed." << std::endl;
		return;
	}

	Json::CharReaderBuilder reader;
	Json::Value root;

	JSONCPP_STRING errs;

	bool res = Json::parseFromStream(reader, ifs, &root, &errs);
	if (!res || !errs.empty())
	{
		std::cout << "parseJson error! " << errs << std::endl;
		return;
	}

	root["Age"] = 30;
	root["Name"] = "Liangbaikai's liangbaikai";
	root["fruit"][0] = "strawberry";
	ifs.close();

	std::ofstream os;
	os.open(file, std::ios::out);
	if (!os.is_open())
	{
		std::cout << "open json file failed." << std::endl;
		return;
	}
	Json::StreamWriterBuilder builder;
	std::unique_ptr writer(builder.newStreamWriter());
	writer->write(root, &os);
	return;
}

main

int main()
{
	std::string jsonStr = MPT_test_createJson();
	MPT_test_saveJson("../json/json-none.json", jsonStr);
	MPT_test_readJson("../json/json-none.json");
	MPT_test_changeJson("../json/json-none.json");
	return 0;
}

你可能感兴趣的:(C/C++)