QJsonObject和QString的互相转化函数

QJsonObject和QString的互相转化函数

  • JsonObj to String
QString json2String(const QJsonObject& json) const
{
    return QString(QJsonDocument(json).toJson(QJsonDocument::Compact));
}
  • String to JsonObj
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;
}

QJsonObject和QString的互相转化函数

上一篇:后台大哥请进一步:使用Visual Studio编译scss和souce map实现前后端的完美结合


下一篇:Fastjson 1.2.24、47 反序列化导致任意命令执行漏洞复现