boost库读写json格式文件

简介

        本文通过boost库,版本为1.84。对json格式文件创建和解析的一个简单的Demo。生成过程中可能会用到库,需要指定库路径。本文通过单个key字段进行值的获取。也有其它方式比如key1.key2.key3.xxx获取值,每一个key代表一个节点。

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/json.hpp>
#include <iostream>

std::string write_json()
{
	boost::property_tree::ptree root;

	{
		boost::property_tree::ptree obj1;
		obj1.put("a", 1);
		obj1.put("b", 2);

		boost::property_tree::ptree obj1_array;

		for (int i = 0; i < 10; ++i)
		{
			boost::property_tree::ptree item;
			item.put("", i);
			obj1_array.push_back(std::make_pair("", item));
		}

		obj1.put_child("obj1_array", obj1_array);

		root.put_child("obj1", obj1);
	}

	{
		boost::property_tree::ptree obj2;

		for (int i = 50; i < 100; i += 10)
		{
			boost::property_tree::ptree item;
			item.put("", i);
			obj2.push_back(std::make_pair("", item));
		}

		root.put_child("obj2_array", obj2);
	}

	std::string file_name = "output.json";
	boost::property_tree::write_json(file_name, root);

	return file_name;
}

void read_json(std::string file_name)
{
	{
		boost::property_tree::ptree root;
		boost::property_tree::read_json(file_name, root);

		std::string _obj1_str = root.get<std::string>("obj1");
		if (_obj1_str.empty())
		{
			std::cout << "_obj1_str is empty." << std::endl;
		}
		else
		{
			std::cout << "_obj1_str is not emptly." << std::endl;
		}

		boost::property_tree::ptree _obj1_node = root.get_child("obj1");
		if (_obj1_node.empty())
		{
			std::cout << "_obj1_node is empty." << std::endl;
		}
		else
		{
			std::cout << "_obj1_node is not emptly." << std::endl;

			for (auto iter = _obj1_node.begin(); iter != _obj1_node.end(); ++iter)
			{
				std::string _key = iter->first;
				std::string _str_value = iter->second.get_value<std::string>(_key);
				if (!_str_value.empty())
				{
					std::cout << "first:" << _key << " " << iter->second.get_value<std::string>(_key) << std::endl;
				}
				else
				{
					for (const auto& item : iter->second)
					{
						std::cout << item.second.get_value<std::string>() << " ";
					}

					std::cout << std::endl;
				}
			}
		}
	}

#if 0
	{
		boost::property_tree::ptree pt;
		boost::property_tree::read_json(file_name, pt);

		std::cout << "obj1.a: " << pt.get<std::string>("obj1.a") << std::endl;
		std::cout << "obj1.b: " << pt.get<std::string>("obj1.b") << std::endl;
		std::cout << "obj1.obj1_array: ";
		for (const auto& item : pt.get_child("obj1.obj1_array")) {
			std::cout << item.second.get_value<std::string>() << " ";
		}
		std::cout << std::endl;

		std::cout << "obj2_array: ";
		for (const auto& item : pt.get_child("obj2_array")) {
			std::cout << item.second.get_value<std::string>() << " ";
		}
		std::cout << std::endl;
	}
#endif

}

int main()
{
	std::string file_name = write_json();

	read_json(file_name);

	return 0;
}

相关推荐

  1. boostjson格式文件

    2024-01-19 06:12:02       33 阅读
  2. C++ 使用nlohmann/json.hppjson字符串

    2024-01-19 06:12:02       12 阅读
  3. python-ConfigParser-配置文件

    2024-01-19 06:12:02       33 阅读
  4. [json]定义、

    2024-01-19 06:12:02       33 阅读
  5. Android中C++如何json文件

    2024-01-19 06:12:02       10 阅读
  6. OpenCV支持哪些类型的文件格式

    2024-01-19 06:12:02       16 阅读
  7. Boostxml

    2024-01-19 06:12:02       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-19 06:12:02       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-19 06:12:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-19 06:12:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-19 06:12:02       18 阅读

热门阅读

  1. 【SpringBoot系列】一键解决跨域问题

    2024-01-19 06:12:02       35 阅读
  2. $route和$router的区别

    2024-01-19 06:12:02       30 阅读
  3. Golang 网络编程

    2024-01-19 06:12:02       21 阅读
  4. python声明和定制构建初始化基本元类

    2024-01-19 06:12:02       28 阅读
  5. 11.25 校招 实习 内推 面经

    2024-01-19 06:12:02       33 阅读
  6. springboot 整合 actuator监控详情

    2024-01-19 06:12:02       28 阅读
  7. 笨蛋学设计模式结构型模式-适配器模式【7】

    2024-01-19 06:12:02       29 阅读
  8. OpenAI/ChatGPT Plus 支持的虚拟卡有哪些

    2024-01-19 06:12:02       124 阅读
  9. linux tty 驱动之ioctls 函数

    2024-01-19 06:12:02       29 阅读
  10. 2. 分享三篇早期的FPGA 布局布线论文

    2024-01-19 06:12:02       32 阅读