1. Qt Json
例子一
我觉得认真看例子是能看懂的!
博客地址:https://www.cnblogs.com/wanghongyang
#include "qjson.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTextStream>
void QJsonTest::buildAndSaveCompleteJsonData()
{
QJsonObject jsonRoot;
// add a boolean value
jsonRoot["isBoolean"] = true;
// add a string value
jsonRoot["addString"] = "stringHere";
// add a int value
jsonRoot["addInt"] = 123;
// add a double value
jsonRoot["addDouble"] = 3.1415926;
// add a json array
QJsonArray jsonArray;
jsonArray.append("Hello");
jsonArray.append("Qt");
jsonArray.append("Json");
jsonArray.append("Array");
jsonRoot["jsonArray"] = jsonArray;
// add another json object
QJsonObject jsonAnotherObject;
jsonAnotherObject["subJsonObject"] = "Hello another object";
jsonRoot["anotherObject"] = jsonAnotherObject;
saveJsonDataToFile(jsonRoot);
}
bool QJsonTest::saveJsonDataToFile(QJsonObject const &jsonObj)
{
QJsonDocument jsonDoc(jsonObj);
QString jsonString = jsonDoc.toJson(QJsonDocument::Indented);
QFile saveFile("json.txt");
if (!saveFile.open(QFile::WriteOnly))
{
return false;
}
saveFile.write(jsonString.toLocal8Bit());
saveFile.close();
return true;
}
void QJsonTest::parseJsonDataFromFile()
{
QFile openFile("json.txt");
if (!openFile.exists())
return;
openFile.open(QFile::ReadOnly | QFile::Text);
// get all file data to a Text Stream and convert it to QByteArray
QTextStream inStream(&openFile);
QString jsonString = inStream.readAll();
// make QJsonDocument from QByteArray
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toLocal8Bit());
QJsonObject jsonRoot = jsonDoc.object();
// parse boolean value
bool booleanData = jsonRoot["isBoolean"].toBool();
// parse string value
QString stringData = jsonRoot["addString"].toString();
// parse double value
double doubleData = jsonRoot["addDouble"].toDouble();
// parse int value
int intData = jsonRoot["addInt"].toInt();
// parse jsonArray value
QJsonArray jsonArrayData = jsonRoot["jsonArray"].toArray();
std::for_each(jsonArrayData.begin(), jsonArrayData.end(), [](QJsonValue const &valueItem) {
const QString arrayItemString = valueItem.toString();
});
// parse json sub Object
QJsonObject jsonSubObj = jsonRoot["anotherObject"].toObject();
QString subObjString = jsonSubObj["subJsonObject"].toString();
}
自己对照上面的写了个例子
#include <QCoreApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
#include <QJsonValue>
void writeJson() {
QJsonObject parent;
QJsonObject obj;
obj.insert("Name", "Ace");
obj.insert("PassWord", "man");
QJsonArray array;
array.append("Sabo");
array.append("Monkey D. Luffy");
obj.insert("chats", array);
QJsonObject obj1;
obj1.insert("Name", "boooK");
obj1.insert("PassWord", "123456");
QJsonArray array1;
array1.append("Sabo");
array1.append("Monkey D. Luffy");
obj1.insert("chats", array1);
parent.insert("Ace", obj);
parent.insert("boooK", obj1);
QJsonDocument doc(parent);
QByteArray json = doc.toJson();
QFile file("E:\\Project\\Qt\\QJson\\data.json");
file.open(QFile::WriteOnly);
file.write(json);
file.close();
}
void readJson() {
QFile file("E:\\Project\\Qt\\QJson\\data.json");
file.open(QFile::ReadOnly);
QByteArray json = file.readAll();
file.close();
QJsonDocument doc = QJsonDocument::fromJson(json);
QJsonObject obj = doc.object();
QStringList keys = obj.keys();
for(int i = 0; i < keys.size(); ++i) {
QString key = keys.at(i);
QJsonValue value = obj.value(key);
QJsonObject obj1 = value.toObject();
// 获得子项的key
//QStringList ls = obj.keys();
//for(int j=0; i<ls.size(); ++j){
// QJsonValue subVal = obj1.value(ls.at(i));
//}
QJsonValue tmp = obj1.value("Name");
QString name = tmp.toString();
tmp = obj1.value("PassWord");
QString password = tmp.toString();
tmp = obj1.value("chats");
QJsonArray array = tmp.toArray();
QStringList list;
for(int j = 0; j < array.size(); j++) {
list.append(array[j].toString());
}
qDebug() << "Name: " << name << " Password: " << password << endl;
qDebug() << "chats: " << list;
}
}
int main(int argc, char* argv[]) {
QCoreApplication a(argc, argv);
writeJson();
readJson();
return a.exec();
}
数据如下: