How to convert a QJsonObject to QString

gogo000 picture gogo000 · Jan 27, 2015 · Viewed 40.1k times · Source

I have a QJsonObject data and want to convert to QString. How can I do this? Searched for help in Qt, it only can convert QJsonObject to QVariantMap...

Thanks in advance.

Answer

TheDarkKnight picture TheDarkKnight · Jan 28, 2015

Remembering when I first needed to do this, the documentation can be a bit lacking and assumes you have knowledge of other QJson classes.

To obtain a QString of a QJsonObject, you need to use the QJsonDocument class, like this: -

QJsonObject jsonObj; // assume this has been populated with Json data

QJsonDocument doc(jsonObj);
QString strJson(doc.toJson(QJsonDocument::Compact));