包含目录:
#include <QJsonObject>
#include <QJsonDocument>
#include <QByteArray>
#include <QFile>
#include <QJsonArray>
基本结构:
写json
QJsonObject studentobj;
QJsonArray arrarydata;
QJsonObject subdata;
int id = 01;
QString name = "zhangsan";
QString gender = "Male";
subdata.insert("id", QJsonValue(id));
subdata.insert("name", QJsonValue(name));
subdata.insert("gender", QJsonValue(gender));
arrarydata.append(QJsonValue(subdata));
id = 02;
name = "lisi";
gender = "Male";
subdata.insert("id", QJsonValue(id));
subdata.insert("name", QJsonValue(name));
subdata.insert("gender", QJsonValue(gender));
arrarydata.append(QJsonValue(subdata));
studentobj.insert("Student", QJsonValue(arrarydata));
//写入到文件,名字为info.json
QJsonDocument docs(studentobj);
QByteArray datas = docs.toJson();
QFile files("student.json");
bool oks = files.open(QIODevice::WriteOnly);
if (oks)
{
files.write(datas);
files.close();
}
else
{
qDebug() << "write error!" << endl;
}
读json
QFile file("student.json");
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
file.close();
//解析
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
if (parseError.error != QJsonParseError::NoError) {
qDebug() << "error";
return;
}
QJsonObject obj = doc.object();
if (obj.contains("Student"))
{
QJsonValue arrayTemp = obj.value("Student");
QJsonArray array = arrayTemp.toArray();
qDebug() << "array size: " << array.size();// size: 2
QJsonValue sub = array.at(0);//因为里面就一个元素,如果有多个元素,可以通过array.size()遍历
QJsonObject subObj = sub.toObject();
int id = subObj.value("id").toInt();
QString name = subObj.value("name").toString();
QString gender = subObj.value("gender").toString();
}
复合结构:
QJsonArray ArrayALL;
QJsonObject studentobj;
QJsonArray studentarrarydata;
QJsonObject studentsubdata;
int id = 01;
QString name = "zhangsan";
QString gender = "Male";
studentsubdata.insert("id", QJsonValue(id));
studentsubdata.insert("name", QJsonValue(name));
studentsubdata.insert("gender", QJsonValue(gender));
studentarrarydata.append(QJsonValue(studentsubdata));
id = 02;
name = "lisi";
gender = "Male";
studentsubdata.insert("id", QJsonValue(id));
studentsubdata.insert("name", QJsonValue(name));
studentsubdata.insert("gender", QJsonValue(gender));
studentarrarydata.append(QJsonValue(studentsubdata));
studentobj.insert("Student", QJsonValue(studentarrarydata));
QJsonObject scoreobj;
QJsonArray scorearrarydata;
QJsonObject scoresubdata;
id = 01;
double math = 95;
double physics = 85;
double english = 66;
scoresubdata.insert("ID", id);
scoresubdata.insert("Math", math);
scoresubdata.insert("Physics", physics);
scoresubdata.insert("English", english);
scorearrarydata.append(scoresubdata);
id = 02;
math = 85;
physics = 95;
english = 86;
scoresubdata.insert("ID", id);
scoresubdata.insert("Math", math);
scoresubdata.insert("Physics", physics);
scoresubdata.insert("English", english);
scorearrarydata.append(scoresubdata);
scoreobj.insert("Score", scorearrarydata);
ArrayALL.append(studentobj);
ArrayALL.append(scoreobj);
QJsonDocument jsonDoc;
jsonDoc.setArray(ArrayALL);
QByteArray json = jsonDoc.toJson();
//写文件
QFile file0("test.json");
file0.open(QIODevice::WriteOnly);
file0.write(json);
file0.close();
读文件
QFile file("info.json");
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
file.close();
//解析
QJsonParseError parseError;
QJsonDocument document= QJsonDocument::fromJson(data, &parseError);
if (parseError.error != QJsonParseError::NoError) {
qDebug() << "error";
return;
}
if(document.isArray())
{
int Num = document.array().size();
for(int i = 0; i < Num ; ++i)
{
QJsonObject obj = document[i].toObject();
QJsonValue value = obj.value("Student");
int size = value.toArray().size(); //size=2
for(int j = 0 ; j < size;++j)
{
QJsonObject objData = value[j].toObject();
int id = objData .value("id").toInt();
........
}
}
......
}