在QT4.7中使用QJson

发布时间:2026/7/28 19:51:26
在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 } }