天天看點

Qt: json對象轉格式化字元串

Qt開發,需要将json對象以字元串形式存入檔案中,需要對json字元串進行格式化,嘗試了兩種效果,記錄如下:

1. 不換行:

QJsonObject json;
//...
QJsonDocument doc(json);
QString content(doc.toJson(QJsonDocument::Compact));
//寫入檔案...      

效果:

Qt: json對象轉格式化字元串

2. 換行:

QJsonObject json;
//...
QJsonDocument doc(json);
QString content(doc.toJson(QJsonDocument::Indented));
//寫入檔案...      

效果: