基于Ubuntu的Linux系统安装jsoncpp开发包过程以及基本应用认识

目录

jsoncpp安装过程:

执行以下命令:

powershell 复制代码
sudo apt update
sudo apt install libjsoncpp-dev

有可能出现的问题:

1.如果在执行sudo apt update时出现以下信息

powershell 复制代码
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease                                                                              
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                                                                               
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease                           
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease                          
Get:6 https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu1804/x86_64  InRelease [1581 B]
Err:6 https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu1804/x86_64  InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY A4B469963BF863CC
Ign:7 https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu1804/x86_64  InRelease
Hit:8 https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu1804/x86_64  Release
Reading package lists... Done
W: GPG error: https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu1804/x86_64  InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY A4B469963BF863CC
E: The repository 'https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

这是一个 GPG 错误,意味着你的系统无法验证 NVIDIA CUDA 软件仓库的签名,可能是因为缺少公钥。可以尝试使用以下步骤解决该问题:

首先获取公钥

可以在 NVIDIA 的官方网站上找到 CUDA 软件仓库的下载链接。以下是 NVIDIA Developer 网站上 Ubuntu 18.04 的 CUDA 软件仓库的下载链接示例:

https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/

在浏览器中打开此链接,并浏览其中的内容,找到 .pub 文件的下载链接。一旦找到 .pub 文件的下载链接,您可以将其复制并用作 apt-key adv --fetch-keys 命令的参数。

通常情况下,.pub 文件包含的是 GPG 公钥。在这种情况下,可以选择任何一个 .pub 文件来获取公钥。

通过以下命令来导入任何一个 .pub 文件中包含的公钥:

powershell 复制代码
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/3bf863cc.pub

之后再执行该命令即可

powershell 复制代码
sudo apt update

然后进行安装jsoncpp

powershell 复制代码
sudo apt install libjsoncpp-dev

之后就可以执行查看

powershell 复制代码
ls /usr/include/jsoncpp/

jsoncpp基本应用认识:

Json::Value类:

json与外界进行数据中转的一个对象类

重载了大量的操作符:赋值运算符

包含了大量的类型转换函数:

Json::Reader类:实现反序列化

将json格式字符串转换为多个数据对象,存储在Json::Value对象中

cpp 复制代码
parse(std::string &buf, Json::Value &val)

Json::Writer类:实现序列化

将Json::Value对象中的数据序列化成为json格式的字符串

基类--子类:Json::FastWriter / Json::StyledWriter

cpp 复制代码
std::string write(Json::Value &val)

举例:

cpp 复制代码
//g++ -std=c++11 json.cpp -o json_test -ljsoncpp
#include<iostream>
#include<string>
#include<jsoncpp/json/json.h>

//json序列化
std::string seria()
{
        const char *name = "张三";
        int id = 10001;
        const char *sex = "男";
        int score[3] = {88, 77, 99};

        Json::Value val;
        val["姓名"] = name;
        val["学号"] = id;
        val["性别"] = sex;
        for(int i = 0; i < 3; i++)
        {
                val["成绩"].append(score[i]);
        }
        Json::StyledWriter writer;
        std::string str = writer.write(val);
        std::cout << str << std::endl;
        return str;
}
//反序列化
void unseria(std::string &str)
{
        Json::Value val;
        Json::Reader reader;
        bool ret = reader.parse(str, val);
        if(ret == false)
        {
                std::cout << "parse json failed!";
                return ;
        }
        std::cout << val["姓名"].asString() << std::endl;
        std::cout << val["性别"].asString() << std::endl;
        std::cout << val["学号"].asInt() << std::endl;
        int num = val["成绩"].size();
        for(int i = 0; i < num; i++)
        {
                std::cout << val["成绩"][i].asInt() << std::endl;
        }
        return ;
}
int main()
{
        std::string str = seria();
        unseria(str);
        return 0;
}
相关推荐
很㗊几秒前
Linux --- tar命令常见用法
linux·运维·服务器
飞凌嵌入式2 分钟前
嵌入式AI领域的主控选择
linux·arm开发·人工智能·嵌入式硬件
RisunJan3 分钟前
Linux命令-ld(将目标文件连接为可执行程序)
linux·运维·服务器
猫猫的小茶馆9 分钟前
【Linux 驱动开发】四. 平台总线驱动
linux·c语言·arm开发·驱动开发·嵌入式硬件·mcu·物联网
济6179 分钟前
linux--Cortex-A7架构-- Ubuntu20.04
linux·运维·架构
init_236115 分钟前
【HCIE-08】NAT64
linux·服务器·网络
深信达沙箱23 分钟前
如何选择源代码加密软件?应关注哪些核心技术要素
linux·服务器·网络·加密·软件·源代码·沙盒
alex180124 分钟前
ubuntu查看目录文件夹大小
linux·运维·ubuntu
游戏开发爱好者829 分钟前
在 Linux 环境通过命令行上传 IPA 到 App Store,iOS自动化构建与发布
android·linux·ios·小程序·uni-app·自动化·iphone
周公挚友30 分钟前
centos 7.9 nvm 安装nodejs
linux·运维·centos