QJsonObject和QString的互相轉化函數


QJsonObject和QString的互相轉化函數

  • JsonObj to String
QString jsonObj2String(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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM