Qt构建与解析Json示例
本文以B站UP主“明王讲QT”的【QT开发专题-天气预报】中的章节内容作为学习Qt中构建、解析Json的参考方法。
1、Json文本
{ "info": { "asian": true, "captical": "beijing", "founded": 1949 }, "name": "China", "provinces": [ { "captical": "jinan", "name": "shandong" }, { "captical": "hangzhou", "name": "zhejiang" } ] }
2、构建、解析代码
#include <QtCore/QCoreApplication> #include <QDir> #include <QFile> #include <QJsonArray> #include <QJsonDocument> #include <QJsonObject> #include <QJsonValue> void writeJson() { QJsonObject rootObject; //1.插入name字段 rootObject.insert("name", "China"); //2.插入info字段 QJsonObject infoObject; infoObject.insert("captical", "beijing"); infoObject.insert("asian", true); infoObject.insert("founded", 1949); rootObject.insert("info", infoObject); //3.插入provinces字段 QJsonArray provinceArray; //山东 QJsonObject provinceSdObject; provinceSdObject.insert("name", "shandong"); provinceSdObject.insert("captical", "jinan"); //浙江 QJsonObject provinceZjObject; provinceZjObject.insert("name", "zhejiang"); provinceZjObject.insert("captical", "hangzhou"); provinceArray.append(provinceSdObject); provinceArray.append(provinceZjObject); rootObject.insert("provinces", provinceArray); //4.将rootObject转换为字符串 QJsonDocument doc(rootObject); QByteArray json = doc.toJson(); //打印输出 qDebug() << QString(json).toUtf8().data(); //5.将json字符串写入文件 QString dirPath = QCoreApplication::applicationDirPath(); QString path = QDir::cleanPath(dirPath + QDir::separator() + "china.json"); QFile file(path); file.open(QFile::WriteOnly); file.write(json); file.close(); } void readJson() { //1.从文件读取json字符串 QString dirPath = QCoreApplication::applicationDirPath(); QString path = QDir::cleanPath(dirPath + QDir::separator() + "china.json"); QFile file(path); file.open(QFile::ReadOnly); QByteArray json = file.readAll(); file.close(); //2.解析json字符串 QJsonDocument doc = QJsonDocument::fromJson(json); if (!doc.isObject()) { qDebug() << "Not an object"; return; } QJsonObject rootObject = doc.object(); QStringList keys = rootObject.keys(); for (int i=0; i<keys.size(); i++) { QString key = keys[i]; QJsonValue value = rootObject.value(key); if (value.isBool()) { qDebug() << key << ":" << value.toBool(); } else if (value.isString()) { qDebug() << key << ":" << value.toString(); } else if (value.isDouble()) { qDebug() << key << ":" << value.toDouble(); } else if (value.isObject()) { qDebug() << key << ":"; QJsonObject infoObject = value.toObject(); QString captical = infoObject["captical"].toString(); bool asian = infoObject["asian"].toBool(); int founded = infoObject["founded"].toInt(); qDebug() << " captical: " << captical; qDebug() << " asian: " << asian; qDebug() << " founded: " << founded; } else if (value.isArray()) { qDebug() << key << ":"; QJsonArray provinceArray = value.toArray(); for (int i=0; i<provinceArray.size(); i++) { QJsonObject provinceObject = provinceArray[i].toObject(); QString name = provinceObject["name"].toString(); QString captical = provinceObject["captical"].toString(); qDebug() << " name: " << name << ", captical: " << captical; } } } }