QJsonObject与QString转化封装
经常使用QT的同学可能会发现有时候需要json字符串和json对象之间的转换,今天他来了,直接上代码:
QString InfoBase::JsonToString(const QJsonObject& json) const
{
return QString(QJsonDocument(json).toJson(QJsonDocument::Compact));
}
QJsonObject InfoBase::StringToJson(const QString& str) const
{
QJsonObject l_ret;
QJsonParseError l_err;
QJsonDocument l_doc = QJsonDocument::fromJson(str.toUtf8(), &l_err);
if (l_err.error == QJsonParseError::NoError)
{
if (l_doc.isObject())
{
l_ret = l_doc.object();
}
}
return l_ret;
}
走的太远不要忘记为什么出发,你的态度决定你的高度。

浙公网安备 33010602011771号