> 文章列表 > C/C++ 中 JSON 库的使用 (CJSON/nlohmann)

C/C++ 中 JSON 库的使用 (CJSON/nlohmann)

C/C++ 中 JSON 库的使用 (CJSON/nlohmann)

C/C++ 中 JSON 库的使用 (CJSON/nlohmann)

  • 概述
  • cjson
    • 基本操作
      • 从(字符指针)缓冲区中解析出JSON结构
      • 转成成JS字符串(将传入的JSON结构转化为字符串)
      • 将JSON结构所占用的数据空间释放
    • JSON 值的创建
      • 创建一个值类型的数据
      • 创建一个对象(文档)
      • 数组创建以及添加
    • JSON嵌套
      • 向对象中增加键值对
      • 向数组中增加对象
      • 几个能提高操作效率的宏函数
    • 查找JSON值
      • 根据键找json结点
      • 判断是否有key是string的项
      • 返回数组结点array中成员的个数
      • 根据数组下标index取array数组结点的第index个成员
      • 遍历数组
  • nlohmann
    • nlohmann库使用代码示例

概述

纯C环境中使用cjson库,C++环境中也可以使用nlohmann库,本文介绍基本的使用场景,如需更详细的介绍可以查看库官方文档。


nlohmann:

nlohmann库(https://github.com/nlohmann/json)提供了丰富而且符合直觉的接口(https://json.nlohmann.me/api/basic_json/),只需导入头文件即可使用,方便整合到项目中。

CJSON:

JSON: JavaScript Object Notation(JavaScript 对象表示法),是轻量级的存储和交换文本信息的语法,类似 XML . 特点是纯文本(纯字符串)、层级结构、使用数组。
cJson:一个基于 C 语言的 Json 库,它是一个开源项目,github 下载地址:https://github.com/DaveGamble/cJSON
cJson库组成:主要的文件有两个,一个 cJSON.c 一个 cJSON.h。使用时,将头文件 include 进去即可

cjson

typedef struct cJSON {  //cJSON结构体struct cJSON*next,*prev;           /* 遍历数组或对象链的前向或后向链表指针*/struct cJSON *child;                   /*数组或对象的孩子节点*/int type;                                     /* key的类型*/char *valuestring;                       /*字符串值*/int valueint;                                /* 整数值*/double valuedouble;                    /* 浮点数值*/char *string;                               /* key的名字*/
} cJSON_st;
  • 基本操作

  • 从(字符指针)缓冲区中解析出JSON结构

extern cJSON *cJSON_Parse(const char *value);       
//解析一块JSON数据返回cJSON结构, 在使用完之后调用cJSON_Delete函数释放json对象结构。

解析JSON数据包,并按照cJSON结构体的结构序列化整个数据包。

使用该函数会通过malloc函数在内存中开辟一个空间,使用完成需要手动释放。

  • 转成成JS字符串(将传入的JSON结构转化为字符串)

extern char *cJSON_Print(cJSON *item); //可用于输出到输出设备,
使用完之后free(char *)  cJSON_PrintUnformatted(cJSON *item);
//类似,没有格式,即转换出的字符串中间不会有"\\n" "\\t"之类的东西存在. 
  • 将JSON结构所占用的数据空间释放

void cJSON_Delete(cJSON *c)
//会将其下的所有节点的资源一并释放掉!!    
  • JSON 值的创建

  • 创建一个值类型的数据

extern cJSON *cJSON_CreateNumber(double num);//创建 extern cJSON
*cJSON_CreateString(const char *string); 
//创建 extern cJSON *cJSON_CreateArray(void); //创建json数组
  • 创建一个对象(文档)

extern cJSON *cJSON_CreateObject(void);
//创建一个根数据项,之后便可向该根数据项中添加string或int等内容 
  • 数组创建以及添加

cJSON *cJSON_CreateIntArray(const int *numbers,int count);void cJSON_AddItemToArray(cJSON *array, cJSON *item); 
  • JSON嵌套

  • 向对象中增加键值对

 cJSON_AddItemToObject(root, "rows", 值类型数据相关函数());
  • 向对象中增加数组
cJSON_AddItemToObject(root, "rows", cJSON_CreateArray());
//创建一个根数据项,之后便可向该根数据项中添加string或int等内容 
  • 向数组中增加对象

cJSON_AddItemToArray(rows, cJSON_CreateObject());
//向json数组中增加元素/对象
  • 几个能提高操作效率的宏函数

#define cJSON_AddNumberToObject(object,name,n)   cJSON_AddItemToObject(object, name,cJSON_CreateNumber(n))
//向json对象中添加数字,节点名and节点值#define cJSON_AddStringToObject(object,name,s)   cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) 
// 向json对象中添加字符串
  • 查找JSON值

  • 根据键找json结点

extern cJSON *cJSON_GetObjectItem(cJSON *object,const char*string) 
//从cJSON结构体中查找某个子节点名称(键名称),如果查找成功可把该子节点序列化到cJSON结构体中。 
  • 判断是否有key是string的项

extern int cJSON_HasObjectItem(cJSON *object,const char *string){
return cJSON_GetObjectItem(object,string)?1:0; } 
//如果有返回1 否则返回0 
  • 返回数组结点array中成员的个数


extern int cJSON_GetArraySize(cJSON *array); 
  • 根据数组下标index取array数组结点的第index个成员

 
extern cJSON *cJSON_GetArrayItem(cJSON *array,int index);
//返回该成员节点  
  • 遍历数组

#define cJSON_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next)  cJSON_ReplaceItemInObject(json,"data",cJSON_CreateString("hello"))
//用于代替json对象中data元组的值 

nlohmann

使用方式比较简单,下面直接通过示例来说明.


nlohmann库使用代码示例

#pragma once#include <iostream>
#include <fstream>
#include <string>
#include <nlohmann/json.hpp>static bool JsonConfigInit(const std::string& config_file)bool ret = false;std::ifstream cfg_file(config_file);if (!cfg_file.is_open()) {nlohmann::json configjson;                  //创建一个空结构configjson["config_settings"] = { {"bool_value", true},{"file_size", 15},{"info_file", "./tmp.ini"}};//对对象进行初始化std::ofstream(config_file.c_str()) << configjson;std::cout  << "create config json file"<< std::endl;return false;}try{std::cout   <<  "JsonConfigInit  from \\" " << config_file<< "\\" "<< std::endl;const nlohmann::json&  file_json = nlohmann::json::parse(cfg_file);//判断首节点是否存在if(file_json.is_null() || (file_json.contains("config_settings") == false ) || file_json.at("config_settings").size() == 0){ret = false; goto json_end;}nlohmann::json  set_json = file_json.at("config_settings");      //	获取相应的键值对,get_to必须保证双方类型一致,否则极易出现段错误bool m_json_bool;int m_json_number;std::string m_json_string ;if(set_json.at("bool_value").is_boolean()){set_json.at("bool_value").get_to(m_json_bool);}if(set_json.at("file_size").is_number()){set_json.at("file_size").get_to(m_json_number);}if(set_json.at("info_file").is_string()){set_json.at("info_file").get_to(m_json_string);}ret = true;}}catch (nlohmann::json::parse_error& ex){  std::cerr   << "JsonConfigInit failed:parse error ! ! reason: [" << ex.what() << "]"<< std::endl;}catch (nlohmann::json::exception& ex){std::cerr   << "JsonConfigInit parse_json parse fail! reason: [" << ex.what() << "]"<< std::endl;}json_end: cfg_file.close();return ret;}

读书笔记