C++ 使用Json封装数据和解析数据

2024-06-02 22:32

本文主要是介绍C++ 使用Json封装数据和解析数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用C++和别的语言做交互比较常用的一种数据操作方式就是json。可以到GitHub上下载对应C++的json库源码,我自己下了一套之前的版本,可以正常使用。jsoncpp-src

  • 编译出来的库名:json_vc71_libmtd.lib(debug) | json_vc71_libmt.lib(release)
  • 需要包含的头文件: jsoncpp-src(对应源码)/include/json (该目录下所有文件)
  • 调用库方式
#include "json/json.h"
#ifdef _DEBUG
#pragma comment(lib,"./lib/json_vc71_libmtd.lib")
#else
#pragma comment(lib,"./lib/json_vc71_libmt.lib")
#endif
  • 封装json数据为string
std::string DataToJson()
{
    Json::FastWriter writerinfo;
    Json::Value  writevalueinfo;writevalueinfo["id"]=123;   writevalueinfo["time"]="2017.08.30 00:00:00";    Json::Value  writedata;writedata["count"] = 1;writedata["name"] = "cpp";writevalueinfo["data"]=writedata;    std::string strEmail = writerinfo.write(writevalueinfo);return strEmail;
}示例json:
{"data": {"count": 1,"name": "cpp"},"id": 123,"time": "2017.08.30 00:00:00"
}
  • 解析json数据
void TranslateJson(const string strData)
{// 解析json用Json::ReaderJson::Reader *readerinfo = new Json::Reader(Json::Features::strictMode());// Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array...Json::Value root;       if (readerinfo->parse(strData, root)){if (root["id"].isInt()){int nID = root["id"].asInt();}if (root["time"].isString()){std::string strTime = root["time"].asString();}if (root["data"]["count"].isInt()){int nDataCount = root["data"]["count"].asInt();}if (root["data"]["name"].isString()){std::string strDataName = root["data"]["name"].asString();}}::delete readerinfo;readerinfo = NULL;
}
  • json数组操作
    封装

Json::Value arrayObj;   // 构建对象
for (int i = 0; i < 3; i++)
{Json::Value new_item;new_item["id"] = i;new_item["name"] = "test";arrayObj.append(new_item);  // 插入数组成员
}
示例json:
[{"id": 0,"name": "test"},{"id": 1,"name": "test"},{"id": 2,"name": "test"}
]arrayObj.append(new_item); 改为 arrayObj["array"].append(new_item);
示例json:
{"array": [{"id": 0,"name": "test"},{"id": 1,"name": "test"},{"id": 2,"name": "test"}]
}

解析

void TranslateJson(const string strData)
{// 解析json用Json::ReaderJson::Reader *readerinfo = new Json::Reader(Json::Features::strictMode());// Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array...Json::Value root;       if (readerinfo->parse(strData, root)){//arrayObj.append(new_item);if (root.isArray()){int nArraySize = root.size();   for (int i=0; i<nArraySize; i++){       int nID = root[i]["id"].asInt();std::string strName = root[i]["name"].asString();   }}// arrayObj["array"].append(new_item);if (root["array"].isArray()){int nArraySize = root["array"].size();  for (int i=0; i<nArraySize; i++){       int nID = root["array"][i]["id"].asInt();std::string strName = root["array"][i]["name"].asString();  }}}::delete readerinfo;readerinfo = NULL;
}

这篇关于C++ 使用Json封装数据和解析数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1025267

相关文章

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解