首页 > 编程语言 >【CJsonObject】C++ JSON 解析器使用教程

【CJsonObject】C++ JSON 解析器使用教程

时间:2023-07-18 14:46:12浏览次数:49  
标签:解析器 std const string int C++ bool CJsonObject

能选封装的尽量不使用底层的

一、CJsonObject 简介

CJsonObjectBwar 基于 cJSON 全新开发一个 C++ 版的 JSON 库。

CJsonObject 的最大优势是轻量、简单好用,开发效率极高,尤其对多层嵌套 json 的读取和生成、修改极为方便。

CJsonObject比cJSON简单易用得多,且只要不是有意不释放内存就不会发生内存泄漏。

用CJsonObject的好处在于完全不用专门的文档,头文件即文档,看完Demo立刻就会用,所有函数都十分通俗易懂,最为关键的一点是解析JSON和生成JSON的编码效率非常高。

Github 地址:https://github.com/Bwar/CJsonObject


二、CJsonObject 应用

仅需将克隆下来的 Github 仓库中四个文件添加至项目中即可

  • cJson.h、cJson.c
  • CJsonObject.hpp、 CJsonObject.cpp
// 实际需要导入的头文件
#include "CJsonObject.hpp"

三、Github 自带的Demo

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "../CJsonObject.hpp"

//int main()
int main(int argc, char* argv[])
{
    std::ifstream fin(argv[1]);
    if (fin.good())
    {
        neb::CJsonObject oJson;
        std::stringstream ssContent;
        ssContent << fin.rdbuf();
        if (oJson.Parse(ssContent.str()))
        {
            std::cout << oJson.ToString() << std::endl;
        }
        else
        {
            std::cerr << "parse json error" << "\n";// << ssContent.str() << std::endl;
        }
        fin.close();
    }
    int iValue;
    double fTimeout;
    std::string strValue;
    neb::CJsonObject oJson("{\"refresh_interval\":60,"
                        "\"test_float\":[18.0, 10.0, 5.0],"
                        "\"test_int\":[135355, -1844674407370955161, -935375],"
                        "\"timeout\":12.5,"
                        "\"dynamic_loading\":["
                            "{"
                                "\"so_path\":\"plugins/User.so\", \"load\":false, \"version\":1,"
                                "\"cmd\":["
                                     "{\"cmd\":2001, \"class\":\"neb::CmdUserLogin\"},"
                                     "{\"cmd\":2003, \"class\":\"neb::CmdUserLogout\"}"
                                "],"
                                "\"module\":["
                                     "{\"path\":\"im/user/login\", \"class\":\"neb::ModuleLogin\"},"
                                     "{\"path\":\"im/user/logout\", \"class\":\"neb::ModuleLogout\"}"
                                "]"
                             "},"
                             "{"
                             "\"so_path\":\"plugins/ChatMsg.so\", \"load\":false, \"version\":1,"
                                 "\"cmd\":["
                                      "{\"cmd\":2001, \"class\":\"neb::CmdChat\"}"
                                 "],"
                             "\"module\":[]"
                             "}"
                        "]"
                    "}");
     std::cout << oJson.ToString() << std::endl;
     std::cout << "-------------------------------------------------------------------" << std::endl;
     std::cout << oJson["dynamic_loading"][0]["cmd"][1]("class") << std::endl;
     oJson["dynamic_loading"][0]["cmd"][0].Get("cmd", iValue);
     std::cout << "iValue = " << iValue << std::endl;
     oJson["dynamic_loading"][0]["cmd"][0].Replace("cmd", -2001);
     oJson["dynamic_loading"][0]["cmd"][0].Get("cmd", iValue);
     std::cout << "iValue = " << iValue << std::endl;
     oJson.Get("timeout", fTimeout);
     std::cout << "fTimeout = " << fTimeout << std::endl;
     oJson["dynamic_loading"][0]["module"][0].Get("path", strValue);
     std::cout << "strValue = " << strValue << std::endl;
     std::cout << "-------------------------------------------------------------------" << std::endl;
     oJson.AddEmptySubObject("depend");
     oJson["depend"].Add("nebula", "https://github.com/Bwar/Nebula");
     oJson["depend"].AddEmptySubArray("bootstrap");
     oJson["depend"]["bootstrap"].Add("BEACON");
     oJson["depend"]["bootstrap"].Add("LOGIC");
     oJson["depend"]["bootstrap"].Add("LOGGER");
     oJson["depend"]["bootstrap"].Add("INTERFACE");
     oJson["depend"]["bootstrap"].Add("ACCESS");
     std::cout << oJson.ToString() << std::endl;
     std::cout << "-------------------------------------------------------------------" << std::endl;
     std::cout << oJson.ToFormattedString() << std::endl;

     std::cout << "-------------------------------------------------------------------" << std::endl;
     neb::CJsonObject oCopyJson = oJson;
     if (oCopyJson == oJson)
     {
         std::cout << "json equal" << std::endl;
     }
     oCopyJson["depend"]["bootstrap"].Delete(1);
     oCopyJson["depend"].Replace("nebula", "https://github.com/Bwar/CJsonObject");
     std::cout << oCopyJson.ToString() << std::endl;
     std::cout << "-------------------------key traverse------------------------------" << std::endl;
     std::string strTraversing;
     while(oJson["dynamic_loading"][0].GetKey(strTraversing))
     {
         std::cout << "traversing:  " << strTraversing << std::endl;
     }
     std::cout << "---------------add a new key, then key traverse---------------------" << std::endl;
     oJson["dynamic_loading"][0].Add("new_key", "new_value");
     while(oJson["dynamic_loading"][0].GetKey(strTraversing))
     {
         std::cout << "traversing:  " << strTraversing << std::endl;
     }

     std::cout << oJson["test_float"].GetArraySize() << std::endl;
     float fTestValue = 0.0;
     for (int i = 0; i < oJson["test_float"].GetArraySize(); ++i)
     {
         oJson["test_float"].Get(i, fTestValue);
         std::cout << fTestValue << "\t in string: " << oJson["test_float"](i) << std::endl;
     }
     for (int i = 0; i < oJson["test_int"].GetArraySize(); ++i)
     {
         std::cout << "in string: " << oJson["test_int"](i) << std::endl;
     }
     oJson.AddNull("null_value");
     std::cout << oJson.IsNull("test_float") << "\t" << oJson.IsNull("null_value") << std::endl;
     oJson["test_float"].AddNull();
     std::cout << oJson.ToString() << std::endl;

     if (oJson.KeyExist("simeout"))
         std::cout << "timeout key exist" << std::endl;

     neb::CJsonObject oLongLong("{\"long_long\":1283949231388184576}");
     int64 llValue = 0;
     uint64 ullValue = 0;
     oLongLong.Get("long_long", llValue);
     oLongLong.Get("long_long", ullValue);
     std::cout << "llValue = " << llValue << ",  ullValue = " << ullValue << std::endl;
     //oJson.Add("json_move", std::move(oLongLong)); // C++11
     oJson.AddWithMove("json_move", oLongLong);  
     std::cout << oJson.ToString() << std::endl;
}

四、用法解析

1、构造方法

CJsonObject();  // 默认的构造方法
CJsonObject(const std::string& strJson);  // 通过Json字符串构造
CJsonObject(const CJsonObject* pJsonObject); // 通过对象指针构造
CJsonObject(const CJsonObject& oJsonObject); // 通过拷贝构造函数构造

2、常用方法

CJsonObject& operator=(const CJsonObject& oJsonObject); // 重载赋值操作符
bool operator==(const CJsonObject& oJsonObject) const; // 重载赋值操作符
bool Parse(const std::string& strJson); // 解析json字符串
void Clear(); // 清除内存
bool IsEmpty() const; // 判断解析对象是否为空
bool IsArray() const; // 判断解析对象是否为json数组
std::string ToString() const; // 将解析对象转换为字符串

3、原始 JSON 对象解析主要使用的方法

bool AddEmptySubObject(const std::string& strKey);  // 在当前的原始解析对象上添加空的子对象
bool AddEmptySubArray(const std::string& strKey);  // 在当前的原始解析对象上添加空的子数组
CJsonObject& operator[](const std::string& strKey); // 重载[]操作符
std::string operator()(const std::string& strKey) const; // 重载()操作符
bool Get(const std::string& strKey, CJsonObject& oJsonObject) const; // 得到与key值相对应的对象键值
bool Get(const std::string& strKey, std::string& strValue) const; // 得到与key值相对应的字符串键值
// 以下的只是根据key值获取不同数据类型的键值
bool Get(const std::string& strKey, int32& iValue) const; 
bool Get(const std::string& strKey, uint32& uiValue) const;
bool Get(const std::string& strKey, int64& llValue) const;
bool Get(const std::string& strKey, uint64& ullValue) const;
bool Get(const std::string& strKey, bool& bValue) const;
bool Get(const std::string& strKey, float& fValue) const;
bool Get(const std::string& strKey, double& dValue) const;
// 在相应的key值上添加不同数据类型的键值
bool Add(const std::string& strKey, const CJsonObject& oJsonObject);
bool Add(const std::string& strKey, const std::string& strValue);
bool Add(const std::string& strKey, int32 iValue);
bool Add(const std::string& strKey, uint32 uiValue);
bool Add(const std::string& strKey, int64 llValue);
bool Add(const std::string& strKey, uint64 ullValue);
bool Add(const std::string& strKey, bool bValue, bool bValueAgain);
bool Add(const std::string& strKey, float fValue);
bool Add(const std::string& strKey, double dValue);
// 删除指定的key值
bool Delete(const std::string& strKey);
// 根据key值替代指定的键值
bool Replace(const std::string& strKey, const CJsonObject& oJsonObject);
bool Replace(const std::string& strKey, const std::string& strValue);
bool Replace(const std::string& strKey, int32 iValue);
bool Replace(const std::string& strKey, uint32 uiValue);
bool Replace(const std::string& strKey, int64 llValue);
bool Replace(const std::string& strKey, uint64 ullValue);
bool Replace(const std::string& strKey, bool bValue, bool bValueAgain);
bool Replace(const std::string& strKey, float fValue);
bool Replace(const std::string& strKey, double dValue);

4、JSON 数据使用的主要方法

int GetArraySize();  // 得到json数组的元素数量
CJsonObject& operator[](unsigned int uiWhich); // 重载[]操作符
std::string operator()(unsigned int uiWhich) const; // 重载()操作符
bool Get(int iWhich, CJsonObject& oJsonObject) const; // 得到数组iWhich索引的解析对象
// 根据索引iWhich不同数据类型的数据
bool Get(int iWhich, std::string& strValue) const;
bool Get(int iWhich, int32& iValue) const;
bool Get(int iWhich, uint32& uiValue) const;
bool Get(int iWhich, int64& llValue) const;
bool Get(int iWhich, uint64& ullValue) const;
bool Get(int iWhich, bool& bValue) const;
bool Get(int iWhich, float& fValue) const;
bool Get(int iWhich, double& dValue) const;
// 往数组添加不同数据类型的键值
bool Add(const CJsonObject& oJsonObject);
bool Add(const std::string& strValue);
bool Add(int32 iValue);
bool Add(uint32 uiValue);
bool Add(int64 llValue);
bool Add(uint64 ullValue);
bool Add(int iAnywhere, bool bValue);
bool Add(float fValue);
bool Add(double dValue);
// 往数组添加不同数据类型的键值作为第一个元素
bool AddAsFirst(const CJsonObject& oJsonObject);
bool AddAsFirst(const std::string& strValue);
bool AddAsFirst(int32 iValue);
bool AddAsFirst(uint32 uiValue);
bool AddAsFirst(int64 llValue);
bool AddAsFirst(uint64 ullValue);
bool AddAsFirst(int iAnywhere, bool bValue);
bool AddAsFirst(float fValue);
bool AddAsFirst(double dValue);
// 删除指定索引iWhich的元素
bool Delete(int iWhich);
// 替代指定索引iWhich的元素
bool Replace(int iWhich, const CJsonObject& oJsonObject);
bool Replace(int iWhich, const std::string& strValue);
bool Replace(int iWhich, int32 iValue);
bool Replace(int iWhich, uint32 uiValue);
bool Replace(int iWhich, int64 llValue);
bool Replace(int iWhich, uint64 ullValue);
bool Replace(int iWhich, bool bValue, bool bValueAgain);
bool Replace(int iWhich, float fValue);
bool Replace(int iWhich, double dValue);

五、个人 Demo

1、JSON 参考

{
	"requestCode": "200",
	"statusCode": 0,
	"args": 
	{
		"name": "Koshkaaa",
		"age": 22,
		"id": 0
	}
}

2、JSON 拼接

#include <iostream>

#include "CJsonObject.hpp"

using namespace std;


int main()
{
	neb::CJsonObject inputJsonObject;
	inputJsonObject.Add("requestCode", "200");
	inputJsonObject.Add("statusCode", 0);

	neb::CJsonObject argsJsonObject;
	argsJsonObject.Add("name", "Koshkaaa");
	argsJsonObject.Add("age", 22);
	argsJsonObject.Add("id", 0);

	inputJsonObject.Add("args", argsJsonObject);

	std::cout << "拼凑的Json为:" << inputJsonObject.ToString() << std::endl;

	getchar();
	return 0;
}

3、JSON 解析

#include <iostream>

#include "CJsonObject.hpp"

using namespace std;


int main()
{
	std::string json = "{"
		"\"requestCode\": \"200\","
		"\"statusCode\": 0,"
		"\"args\": "
	"{"
		"\"name\": \"Koshkaaa\","
			"\"age\": 22,"
			"\"id\": 0"
	"}"
	"}";

	neb::CJsonObject inputJsonObject(json);

	std::string requestCode;
	int statusCode;

	inputJsonObject.Get("requestCode", requestCode);
	inputJsonObject.Get("statusCode", statusCode);

	neb::CJsonObject argsJsonObject;
	inputJsonObject.Get("args", argsJsonObject);
	if (!argsJsonObject.IsEmpty())
	{
		std::string name;
		int age;
		int id;

		argsJsonObject.Get("name", name);
		argsJsonObject.Get("age", age);
		argsJsonObject.Get("id", id);
	}

	getchar();
	return 0;
}

参考文章

标签:解析器,std,const,string,int,C++,bool,CJsonObject
From: https://www.cnblogs.com/RioTian/p/17562969.html

相关文章

  • C++ 网络编程 asio 使用总结
    概述Asio是一个用于网络和低级I/O编程的跨平台C++库,它使用现代C++方法为开发人员提供一致的异步模型.io_contextio_context类为异步I/O对象的用户提供了核心I/O功能,包含:asio::ip::tcp::socketasio::ip::tcp::acceptorasio::ip::udp::socketasio::deadline_timer......
  • 109.C++类内初始化
    109.C++类内初始化C++11规定,可以为数据成员提供一个类内初始值。创建对象时,类内初始值用于初始化数据成员。像下面这样,cursor和height的类内初始值均为0。classScreen{private: intcursor=0; intheight=0;};1.不能用圆括号给类内初始值的原因C++primer(第5版)中......
  • C++学生健康信息收集系统[2023-07-18]
    C++学生健康信息收集系统[2023-07-18]学生健康信息收集系统简介一、 问题描述为了应对新型冠状病毒疫情,学校需要开发一个能够每天收集全校学生健康信息的系统,便于学校管理。不同学院以及学校的管理员,需要能方便地查看和导出健康状况异常的学生列表,并能对各类信息进行查看和统计......
  • C/C++用电管理数据[2023-07-18]
    C/C++用电管理数据[2023-07-18]用visualstudioc++设计一款程序来统计用电管理数据,要求能用菜单实现如下功能:(1)输入每个电表的用户名,楼栋号,抄表日期,电表读数。(3)按作者的用电量,从高到低排出每个用户的总用电量。(3)根据用户要求输出某用户某月(从键盘输入用户名和月份)的总用电量。......
  • C++语言程序设计任务书[2023-07-18]
    C++语言程序设计任务书[2023-07-18]C++语言程序设计任务书指导老师:李力课程编号:一、学时与学分学时:40学分:2二、实践目的计算机实践是本科计算机基础教学的一个重要环节。它对于巩固学生的计算机基础知识,增强学生的计算机应用水平,改善学生的知识结构,具有重要意义。三、......
  • C/C++文件加密解密[2023-07-18]
    C/C++文件加密解密[2023-07-18]题目27:文件加密文件的传输会有明文和密文的区别,明文发送是不安全的,用一个程序实现发送文件的加密和解密操作。加密算法,密钥设计由同学自己选择现有的加密解密算法或是自己设计。要求:(1)对文件的字符根据加密算法,实现文件加密。(2)对操作给出必......
  • C/C++学生成绩管理系统[2023-07-18]
    C/C++学生成绩管理系统[2023-07-18]学生成绩管理系统开发一个可以管理学生成绩以及学生基本信息的一个信息系统,至少实现如下功能:信息管理,支持信息的增、删、改、查操作,具体信息类型如下:(1) 管理学生信息 ,包括学号,姓名,年龄,班级等等信息。(2) 班级信息,包括班级编号、班级人数,......
  • C/C++电影评分系统[2023-07-18]
    C/C++电影评分系统[2023-07-18]程序设计综合课程设计指导书一、题目:电影评分系统二、设计内容及要求:根据C++课程所学的概念、理论和方法,按照C++程序设计的基本步骤,设计出一个适当规模的程序来实现设计课程内容中的全部功能。本系统要求模拟实现电影评分系统,其中包括电影资源......
  • c++环形队列的简单实现
    环形队列可以通过维护count来间接维护tail和head指针的关系,简化程序,避免了直接使用tail和head指针,读写时head与tail回环时的比较处理,判断队列元素长度时的复杂处理,如下为不基于count而是直接使用head和tail指针比较的环形队列的实现,逻辑较为复杂uint32_tCAudioRingBuffer::Da......
  • HandlerMethodArgumentResolver方法参数解析器的使用
    一、使用场景介绍HandlerMethodArgumentResolver,中文称为方法参数解析器,是SpringWeb(SpringMVC)组件中的众多解析器之一,主要用来对Controller中方法的参数进行处理。在一般的接口调用场景下,每次调用Controller都需要检查请求中的token信息,并根据token还原用户信息,然后将用户信息封......