QJsonObject和QString的互相转化函数

时间:2020-06-28 15:57:57   收藏:0   阅读:443

QJsonObject和QString的互相转化函数

QString json2String(const QJsonObject& json) const
{
    return QString(QJsonDocument(json).toJson(QJsonDocument::Compact));
}
QJsonObject string2JsonObj(const QString& str) const
{
    QJsonObject ret;

    QJsonParseError err;
    QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8(), &err);
    if (err.error == QJsonParseError::NoError)
    {
        //qDebug() << __FUNCTION__ << __LINE__ << "No Error!!!";
        if (doc.isObject())
        {
            //qDebug() << __FUNCTION__ << __LINE__ << "doc isObject";
            ret = doc.object();
        }
        else
        {
            qDebug() << __FUNCTION__ << __LINE__ << "doc is not Object";
        }
    }
    else 
    {
        qDebug() << __FUNCTION__ << __LINE__ << "Error!!!";
    }
    return ret;
}

原文:https://www.cnblogs.com/linkyip/p/13202861.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!