在QT4.7中使用QJson

前言

在用QT4.7.0写一个小程序,有http get的操作,返回结果是json格式,需要从json串中拿key-value。
QT4中没有json组件, 查资料,说有QJson可用, 我用的QJson0.8.1
https://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 <[email protected]>

    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")]; // can't 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"
    }
}

发布了436 篇原创文章 · 获赞 126 · 访问量 175万+

猜你喜欢

转载自blog.csdn.net/LostSpeed/article/details/100183683
今日推荐