jsoncpp 使用说明(ubuntu)

1、安装:

sudo apt install -y libjsoncpp-dev

2、配置:

Cmakelist配置:

set(JSONCPP_INCLUDE_DIRS /path/to/jsoncpp/include)

set(JSONCPP_LIBRARIES /path/to/jsoncpp/lib/libjsoncpp.so)

find_package(PkgConfig REQUIRED)

pkg_check_modules(JSONCPP REQUIRED jsoncpp)

include_directories(${JSONCPP_INCLUDE_DIRS})

target_link_libraries(your_executable_name ${JSONCPP_LIBRARIES})

3、使用:

JsonCpp 主要包含三种类型的 class:

Json::Value:可以表示所有支持的类型,如:int , double ,string , object, array等。其包含节点的类型判断(isNull,isBool,isInt,isArray,isMember,isValidIndex等),类型获取(type),类型转换(asInt,asString等),节点获取(get,[]),节点比较(重载<,<=,>,>=,==,!=),节点操作(compare,swap,removeMember,removeindex,append等)等函数。

Json::Reader:将文件流或字符串创解析到Json::Value中,主要使用parse函数。Json::Reader的构造函数还允许用户使用特性Features来自定义Json的严格等级。

Json::Writer:与JsonReader相反,将Json::Value转换成字符串流等,Writer类是一个纯虚类,并不能直接使用。在此我们使用 Json::Writer 的子类:Json::FastWriter(将数据写入一行,没有格式),Json::StyledWriter(按json格式化输出,易于阅读)

4、参考示例:

复制代码
{
  "content": "Hello JsonCpp"}

使用 Reader 和 Value 读取 content 的值:

复制代码
#include <string>
#include <iostream>
#include <json/json.h>

int main(int argc, char const *argv[])
{
  
    std::string str = "{\"content\": \"Hello JsonCpp\"}";

    Json::Reader reader;
    Json::Value root;
    if (reader.parse(str, root))
        std::cout << root["content"].asString() << std::endl;
    
    return 0;
}

调用 Reader.parse() 接口尝试解析 json 字符串 str,当 str 满足 json 格式之后,调用 Value[] 操作符将 "content" 的值取出来,然后再进行类型转换,取出实际的类型数据。

值类型

JsonCpp 支持的值类型总共有 8 种:

enum ValueType Description
0 nullValue 'null' value
1 intValue signed integer value
2 unsigned int unsigned integer value
3 realValue double value
4 stringValue UTF-8 string value
5 booleanValue bool value
6 arrayValue array value (ordered list)
7 objectValue object value (collection of name/value pairs)

类型判断

如果需要进行类型判断,Json::Value 已经提供了完备的类型判断接口供调用:

复制代码
class Value {
  
    ...
    bool isNull() const;
    bool isBool() const;
    bool isInt() const;
    bool isInt64() const;
    bool isUInt() const;
    bool isUInt64() const;
    bool isIntegral() const;
    bool isDouble() const;
    bool isNumeric() const;
    bool isString() const;
    bool ihttps://www.coonote.com/linux/sar-find-bottleneck.htmlray() const;
    bool isObject() const;
    ...
};

其中这里有两个接口比较特殊,一个是 isNumeric() 另一个是 isIntegral()

先对 isNumeric() 进行说明,字面意思就是"是否为数字",实际上在 Json::Value 类的实现中等同于 isDouble(),因此这两个函数是等效的

键值判断

Value.isMember() 接口用于判断 json 字符串中是否存在某个键值,

参考地址:

JsonCpp 使用指导-菜鸟笔记 (coonote.com)

相关推荐
乾元5 小时前
网络遥测(Telemetry/gNMI)的结构化建模与特征化体系—— 从“采集指标”到“可被 AI 推理的状态向量”
运维·服务器·网络·人工智能·网络协议·华为·ansible
一个写python的菜鸟5 小时前
华为服务器安装Todesk
linux·运维·服务器
小曾同学.com5 小时前
认识Linux 的shebang行,一个特殊的注释行
linux·解释器·shebang·/usr/bin/env
有谁看见我的剑了?5 小时前
Vmware vSphereClient为虚机安装vmtools
运维·云计算
Thexhy5 小时前
CentOS7安装Redis全攻略
linux·经验分享·redis·学习
数字冰雹5 小时前
从“被动响应”到“主动洞察”:数字孪生如何重塑数据中心运维
运维
网硕互联的小客服5 小时前
CC攻击对服务器正常运行会有什么影响?如何预防和解决CC攻击?
运维·服务器·网络·windows·安全
中科米堆5 小时前
机器人企业采用自动化三维测量方案,完成关节部件快速检测-中科米堆CASAIM
运维·机器人·自动化·3d全尺寸检测
九鼎创展科技5 小时前
「有温度的陪伴」:基于全志 V821 的情感共鸣型实体机器人详解
linux·人工智能·嵌入式硬件·机器人
色空大师5 小时前
【linux查看日志】
java·linux·运维·日志