自学JSON过程,内容来自B站收藏夹。
#include <QCoreApplication>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonValue>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QJsonParseError>
/*
{
"name": "milo",
"age": 80,
"professional": { //{}表示一个对象
"english": 4,
"putonghua": 2,
"computer": 3
},
"languages": ["C++", "C"],
"phone": {
"number": "19818962390",
"type": "home"
},
"course": [{
"name": "Linux kernel development",
"price": 7.7
}, {
"name": "Linux server development",
"price": 8.8
}],
"vip": true,
"address": null
}
*/
//实现对JSON的封装,形成一个string字符串
void test1()
{
QJsonObject professionalObj;
professionalObj.insert("english", 4);
professionalObj.insert("putonghua", 2);
professionalObj.insert("computer", 3);
QJsonArray languagesArr;
languagesArr.append("C++");
languagesArr.append("C");
QJsonObject phoneObj;
phoneObj.insert("number", 19818962390);
phoneObj.insert("type", "home");
QJsonObject courseObj1;
courseObj1.insert("name", "Linux kernel development");
courseObj1.insert("price", 7.7);
QJsonObject courseObj2;
courseObj2.insert("name", "Linux server development");
courseObj2.insert("price", 8.8);
QJsonArray courseArr;
courseArr.append(courseObj1);
courseArr.append(courseObj2);
QJsonObject rootObj;
rootObj.insert("name", "milo");
rootObj.insert("age", 80);
rootObj.insert("professional", professionalObj);
rootObj.insert("languages", languagesArr);
rootObj.insert("phone", phoneObj);
rootObj.insert("course", courseArr);
rootObj.insert("vip", true);
rootObj.insert("address", QJsonValue::Null);
QJsonDocument doc;
doc.setObject(rootObj);
QByteArray byteArr = doc.toJson();
QString jsonStr = QString(byteArr);
qDebug() << jsonStr;
//将Json字符串保存到指定位置形成Json文件,但是顺序是无序的
QFile file("D:\\temp.json");
if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
{
qDebug() << "can't open, error!";
}else{
QTextStream stream(&file);
stream << jsonStr << "\n";
file.close();
}
}
//从指定文件中读取JSON
void test2()
{
qDebug() << "======================";
QFile file("D:\\temp.json");
if(!file.open(QFile::ReadWrite))
{
return;
}
QByteArray byteArr = file.readAll();
QString jsonStr = QString(byteArr);
qDebug() << jsonStr;
//解析json
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(byteArr, &err); //需要两个参数
QJsonObject rootObj = doc.object();
if(err.error != QJsonParseError::NoError)
{
qDebug() << "Json格式错误";
return;
}else{
//解析json
QJsonValue nameValue = rootObj.value("name");
qDebug() << nameValue.toString(); //因为name为string,故toString
QJsonValue ageValue = rootObj.value("age");
qDebug() << ageValue.toInt();
QJsonValue professionalValue = rootObj.value("professional");
if(professionalValue.type() == QJsonValue::Object)
{
QJsonObject professionalObj = professionalValue.toObject();
QJsonValue englishValue = professionalObj.value("english");
qDebug() << englishValue.toInt();
QJsonValue putonghuaValue = professionalObj.value("putonghua");
qDebug() << putonghuaValue.toInt();
QJsonValue computerValue = professionalObj.value("computer");
qDebug() << computerValue.toInt();
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//test1();
test2();
return a.exec();
}