nlohmann json:map<string,variant>与json object之间的互转

386 篇文章 ¥159.90 ¥299.90
博客介绍了如何在C++中使用nlohmann json库将map<string, variant>类型的数据转换为json object,反之亦然。通过示例展示了转换过程,最终成功实现了数据类型的相互转化。" 72312484,4930519,Django ORM API实践:模型与自定义方法,"['Python', 'Django', '数据库开发', 'ORM']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

json的object的key是string,但是值可以是任意类型,与C++中的普通map并不一致,因此对应的是map<string,variant>

基于:

nlohmann json:variant与json之间的互转_风静如云的博客-CSDN博客

 可通过如下方式完成转换:

#include <iostream>
#include <string>
#include <variant>
#include <map>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
using namespace std;

namespace nlohmann {

template <typename T, typename... Ts>
void variant_from_json(const nlohmann::json &j, std::variant<Ts...> &data) {
    try {
        data = j.get<T>();
    } catch (...) {
    }
}
 
template &l
``` class TestData : public QObject { Q_OBJECT public: TestData(QObject *parent = nullptr) : QObject(parent) {} //关键 必须这么写 TestData(const TestData &other) : QObject(other.parent()) { //this = other; // Copy other data members here this->Barcode = other.Barcode; this->CameraCounts = other.CameraCounts; this->GroupDefine = other.GroupDefine; this->Basic = other.Basic; this->Result = other.Result; this->Normal = other.Normal; this->Defocus = other.Defocus; this->m_bBarcode = other.m_bBarcode; this->m_bCameraCounts = other.m_bCameraCounts; this->m_bDefocus = other.m_bDefocus; this->m_bGroupDefine = other.m_bGroupDefine; } //Person &operator=(const Person &other) = delete; // 禁用赋值运算符 //关键 必须这么写 TestData &operator=(const TestData &other) { if (this != &other) { //this = other; // Copy other data members here this->Barcode = other.Barcode; this->CameraCounts = other.CameraCounts; this->GroupDefine = other.GroupDefine; this->Basic = other.Basic; this->Result = other.Result; this->Normal = other.Normal; this->Defocus = other.Defocus; this->m_bBarcode = other.m_bBarcode; this->m_bCameraCounts = other.m_bCameraCounts; this->m_bDefocus = other.m_bDefocus; this->m_bGroupDefine = other.m_bGroupDefine; } return *this; } public: QString Barcode; QString CameraCounts; QVector<QString> GroupDefine; BasicData Basic; ResultData Result; NormalData Normal; DefocusData Defocus; bool m_bBarcode; bool m_bCameraCounts; bool m_bGroupDefine; bool m_bDefocus; public: QVariantMap toVariantMap() const { QVariantMap map; if (m_bBarcode) { map["Barcode"] = QVariant::fromValue(Barcode); } if (m_bCameraCounts) { map["CameraCounts"] = QVariant::fromValue(CameraCounts); } if (m_bGroupDefine) { QVariantList list; for (auto item : GroupDefine) { list.append(item); } map["GroupDefine"] = list; } if (m_bDefocus) { QVariantMap tempDefocusMap = Defocus.toVariantMap(); if (tempDefocusMap.size() > 0) { map["Defocus"] = tempDefocusMap; } } QVariantMap tempBasicMap = Basic.toVariantMap(); if (tempBasicMap.size()>0) { map["Basic"] = tempBasicMap; } QVariantMap tempNormalMap = Normal.toVariantMap(); if (tempNormalMap.size() > 0) { map["Normal"] = tempNormalMap; } QVariantMap tempResultMap = Result.toVariantMap(); if (tempResultMap.size() > 0) { map["Result"] = tempResultMap; }```改用MFC+C++,并使用boost增强多类型支持和nlohmann::json实现json序列化
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风静如云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值