jsoncpp使用实例

#JSON文件
编辑一个json文件,里面包含了对象以及数组等信息,文件内容如下:

{ 
    "ip" : "1.2.3.4",
	"port" : "8088",    
	"operDir" : "/tmp/curl/",
	"url" : 
	[
		{
			"name":"raid_info",
			"method" : "get",
			"post_data": ""
		}, 
		{
			"name":"hello",
			"method" : "post",
			"post_data":
			{
				"op":"log_detail",
				"type":"shift",
				"file_name":"/home/log",
				"begin_shift":3,
                "lines_count":2000
			}
		}
	]
}

#读取文件
将数据读入到内存:

static std::string read_file(const char *path)
{
	FILE *file = fopen(path, "rb");
	if ( !file )
		return std::string("");

	fseek(file, 0, SEEK_END);
	long size = ftell(file);
	fseek(file, 0, SEEK_SET);
	std::string text;
	char *buffer = new char[size+1];
	buffer[size] = 0;

	if (fread(buffer, 1, size, file) == (unsigned long)size)
		text = buffer;

	fclose(file);
	delete[] buffer;
	return text;
}

#解析数据
将读入的数据全部放入Json::Reader,解析完毕之后数据就已经解析完毕了:

static int
parse_value(const std::string &input)
{
	Json::Value root;
	const Json::Features features;
	Json::Reader reader(features);
	if (!reader.parse(input, root))
	{
		std::cout << "Failed to parse" << std::endl;
		return 1;
	}

	std::cout << "ip:" << root["ip"].asString() << std::endl;
	std::cout << "post:" << root["port"].asString() << std::endl;
	std::cout << "operDir:" << root["operDir"].asString() << std::endl;
	Json::Value interface = root["url"];
	Json::FastWriter fastWriter;
	for (unsigned int i = 0; i < interface.size(); i++)
	{
		std::cout << "###############" << std::endl;
		std::cout << "name:" << interface[i]["name"].asString() << std::endl;
			std::cout << "method:" << interface[i]["method"].asString() << std::endl;
		Json::Value post_data = interface[i]["post_data"];
	    std::cout << "post_data" << post_data << std::endl;
		
		std::string output = fastWriter.write(post_data);
		std::cout << "output" << output << std::endl;
	}
   return 0;
}

#所有源码如下:

#include "json/json.h"
#include <algorithm>
#include <stdio.h>

static std::string read_file(const char *path)
{
	FILE *file = fopen(path, "rb");
	if ( !file )
		return std::string("");

	fseek(file, 0, SEEK_END);
	long size = ftell(file);
	fseek(file, 0, SEEK_SET);
	std::string text;
	char *buffer = new char[size+1];
	buffer[size] = 0;

	if (fread(buffer, 1, size, file) == (unsigned long)size)
		text = buffer;

	fclose(file);
	delete[] buffer;
	return text;
}


static int
parse_value(const std::string &input)
{
	Json::Value root;
	const Json::Features features;
	Json::Reader reader(features);
	if (!reader.parse(input, root))
	{
		std::cout << "Failed to parse" << std::endl;
		return 1;
	}

	std::cout << "ip:" << root["ip"].asString() << std::endl;
	std::cout << "post:" << root["port"].asString() << std::endl;
	std::cout << "operDir:" << root["operDir"].asString() << std::endl;
	Json::Value interface = root["url"];
	Json::FastWriter fastWriter;
	for (unsigned int i = 0; i < interface.size(); i++)
	{
		std::cout << "###############" << std::endl;
		std::cout << "name:" << interface[i]["name"].asString() << std::endl;
			std::cout << "method:" << interface[i]["method"].asString() << std::endl;
		Json::Value post_data = interface[i]["post_data"];
	    std::cout << "post_data" << post_data << std::endl;
		
		std::string output = fastWriter.write(post_data);
		std::cout << "output" << output << std::endl;
	}
   return 0;
}



int main( int argc, const char *argv[] )
{
	int ret = 0;
	std::string input = read_file("test1.json");
	if (input.empty())
	{
		printf("Failed to read input or empty\n");
		return -1;
	}
	
	ret = parse_value(input);
	return ret;
}

这里写图片描述
源码链接:
https://gitee.com/kslly/jsoncpp.git

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jsoncpp是一个C++ JSON解析库,它可以将JSON数据转换为C++对象,也可以将C++对象转换为JSON数据。 下面是Jsoncpp使用详解: 1. 下载安装 你可以从Jsoncpp的官方网站下载最新的源代码,然后编译安装。也可以使用包管理器进行安装,如在Ubuntu下可以使用以下命令进行安装: ``` sudo apt-get install libjsoncpp-dev ``` 2. 解析JSON数据 Jsoncpp提供了一个Json::Value类,它可以表示JSON数据中的任何类型。使用Jsoncpp解析JSON数据的一般步骤如下: ```c++ #include <iostream> #include <jsoncpp/json/json.h> int main() { std::string json_str = "{\"name\": \"Tom\", \"age\": 18}"; Json::Value root; Json::CharReaderBuilder builder; Json::CharReader* reader = builder.newCharReader(); JSONCPP_STRING errors; bool parsing_successful = reader->parse(json_str.c_str(), json_str.c_str() + json_str.size(), &root, &errors); delete reader; if (!parsing_successful) { std::cerr << "Failed to parse JSON: " << errors << std::endl; return -1; } std::string name = root["name"].asString(); int age = root["age"].asInt(); std::cout << "Name: " << name << ", Age: " << age << std::endl; return 0; } ``` 上面的代码解析了一个JSON字符串,然后获取了其中的"name"和"age"字段的值。 3. 生成JSON数据 Jsoncpp可以将C++对象转换为JSON数据。下面是一个示例: ```c++ #include <iostream> #include <jsoncpp/json/json.h> int main() { Json::Value root; root["name"] = "Tom"; root["age"] = 18; Json::StreamWriterBuilder builder; builder["indentation"] = " "; // 设置缩进 std::cout << Json::writeString(builder, root) << std::endl; return 0; } ``` 上面的代码生成了一个JSON对象,然后将它转换为JSON字符串并输出。 4. 更多用法 Jsoncpp还提供了很多其他的用法,如数组、嵌套对象、注释等等。如果需要了解更多用法,请参考Jsoncpp的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值