// 构建JSON对象
QJsonObject json;
json.insert("state", "error");
json.insert("age", 11);
// 构建JSON文档
QJsonDocument document;
document.setObject(json);
// 将JSON文档转换为字符串
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() <<strJson;
// 将字符串转换为JSON文档
QJsonParseError jsonError;
QJsonDocument _document = QJsonDocument::fromJson(strJson.toStdString().data(), &jsonError);
if (jsonError.error == QJsonParseError::NoError) {
// 获取JSON对象
QJsonObject json = _document.object();
QString state = json["state"].toString();
int age = json["age"].toInt();
qDebug() <<" state="<<state;
qDebug() <<" age="<<age;
}