尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
在QT4.7中使用QJson
前言在用QT4.7.0写一个小程序有http get的操作返回结果是json格式需要从json串中拿key-value。QT4中没有json组件, 查资料说有QJson可用, 我用的QJson0.8.1https://github.com/flavio/qjson/releases/tag/0.8.1试验QJson的包含看了网上同学的QJson用法很多人都将QJson编译成单独的DLL, 供QT4使用。我是QT4新手书看的很少。怎么用简单我就怎么来。看到有同学直接包含进工程用我也这么用。包含后编译不过。在qjson_export.h中定义空的QJSON_EXPORT这样就不会按照DLL编译了然后就能编译过。/* This file is part of the KDE project Copyright (C) 2009 Pino Toscano pinokde.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1, as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef QJSON_EXPORT_H #define QJSON_EXPORT_H #include QtCore/qglobal.h #define QJSON_EXPORT // ! 直径包含进工程用字节定义一个空宏 #ifndef QJSON_EXPORT # if defined(QJSON_MAKEDLL) /* We are building this library */ # define QJSON_EXPORT Q_DECL_EXPORT # else /* We are using this library */ # define QJSON_EXPORT Q_DECL_IMPORT # endif #endif #endif测试代码的调用部分拼一段json串送进函数进行分析这段json串就是QJson官方给的例子串// test parse_replay_as_json str_tmp tr({ \ \encoding\ : \UTF-8\, \ \plug-ins\ : [ \ \python\, \ \c\, \ \ruby\ \ ], \ \indent\ : { \length\ : 3, \use_space\ : true } \ }); rd_buf str_tmp.toUtf8(); // 将字符串转成数组 parse_replay_as_json(rd_buf);测试代码的分析部分QJson中对于不存在的key做了一个空key-value, 返回的值是空.显然这不是想要的结果。去单步QJson的实现发现可以用QVariantMap::contains()去检测是否有key, 如果key存在才去取值。void WorkerThread3::parse_replay_as_json(QByteArray data) { QString str_tmp; // str_tmp.append(data); QJson::Parser parser; bool ok false; // json is a QString containing the data to convert QVariantMap result parser.parse(data, ok).toMap(); if (!ok) { qDebug() tr(error : json parse); } QVariant var1; bool b_key_exist false; // 不存在的值 b_key_exist result.contains(tr(key_not_exist)); if (b_key_exist) { var1 result[tr(key_not_exist)]; // cant run this code, because key not exist } // key - value b_key_exist result.contains(tr(encoding)); if (b_key_exist) { var1 result[tr(encoding)]; qDebug() tr(encoding) var1.toString(); // encoding UTF-8 } // 枚举值 e.g. plug-ins : {a, b, c} b_key_exist result.contains(tr(plug-ins)); if (b_key_exist) { foreach (QVariant plugin, result[plug-ins].toList()) { str_tmp plugin.toString(); qDebug() enum value str_tmp; // enum value python // enum value c // enum value ruby } } // 子map, 里面有key-value QVariantMap nestedMap result[indent].toMap(); b_key_exist nestedMap.contains(tr(length)); if (b_key_exist) { str_tmp nestedMap[tr(length)].toString(); qDebug() tr(length ) str_tmp; // length 3 } b_key_exist nestedMap.contains(tr(use_space)); if (b_key_exist) { str_tmp nestedMap[tr(use_space)].toString(); qDebug() tr(use_space ) str_tmp; // use_space true } }
RELATED

相关推荐

AMAT 0100-20157 PCB 加载器

AMAT 0100-20157 PCB 加载器

AMAT 0100-20157 PCB 加载器,核心特点如下(共15条):中间(15条)应用材料品牌专用PCB加载器组件。用于半导体产线中印刷电路板的自动上料。适配AMAT多款半导体制造设备平台。采用模块化独立控制系统&#xff…

📅 2026/8/23 2:30:59
海南俱乐部管理软件选型的技术判断与核验要点

海南俱乐部管理软件选型的技术判断与核验要点

海南地区校友会、行业商会、兴趣俱乐部的管理软件选型,需立足底层技术逻辑,核心核验四项能力:灵活的多层组织架构、自主可控的会员数据、闭环的活动管理、可追溯的分级权限。“海南”仅为地域使用场景,不会让软件自动具备本地化适…

📅 2026/9/8 0:01:18
AMA 0090-A7720 过滤盒

AMA 0090-A7720 过滤盒

AMA 0090-A7720 过滤盒,核心特点如下(共15条):中间(15条)用于工业设备流体或气体的过滤净化。原厂设计,尺寸与接口规格精准匹配。外壳采用耐腐蚀材料,适应化学品环境。密封性能优良&…

📅 2026/9/8 0:03:22
MORE NEWS

更多资讯

📰

PyPTO vf.truncate 详解:向量寄存器浮点截断指令的语义、实现与实战

人工智能编译器模型编译高性能计算深度学习CANN 【免费下载链接】pypto PyPTO(发音: pai p-t-o):Parallel Tensor/Tile Operation编程范式。 项目地址: https://gitcode.com/cann/pypto 点击查看 免费下载 导读 vf.truncate 是 …

📰

PicoClaw 接入 QQ 开放平台机器人:官方 API 配置、部署与源码解析

人工智能AI 应用AI Agent交互助手工具调用MCP ClientsAgent 记忆 【免费下载链接】picoclaw Tiny, Fast, and Deployable anywhere — automate the mundane, unleash your creativity 项目地址: https://gitcode.com/gh_mirrors/pi/picoclaw 点击查看 免费下载 导…

📰

NumPy 用户自定义 DType 的 Partition / Argpartition 支持:基于 ArrayMethod API 实现自定义分区算法

NumPy 用户自定义 DType 的 Partition / Argpartition 支持:基于 ArrayMethod API 实现自定义分区算法 【免费下载链接】numpy The fundamental package for scientific computing with Python. 项目地址: https://gitcode.com/gh_mirrors/nu/numpy 本篇技术…

📰

EsDA低代码实现Modbus RTU Master转UDP Client协议转换

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

大文件分片上传实战:Blob切片、秒传与断点续传五层追问

1. 大文件分片上传到底在解决什么问题1.1 从一个真实场景说起去年帮一个做在线教育的朋友处理课件上传的问题,他们平台允许老师上传录播视频,单个文件动辄两三个G。最开始用的是最朴素的方式——一个FormData把整个文件塞进去,axios发一个 PO…

📰

C# HttpClient跳过HTTPS证书验证的三种写法与踩坑指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬