当前位置:网站首页>Qt reads Json files (including source code + comments)
Qt reads Json files (including source code + comments)
2022-08-02 18:04:00 【web18484626332】
First, the content of the sample Json file
The following is the content of the Json file used in this article, which contains strings, numbers, booleans, nulls, objects, arrays, combined types, etc. (the reading method is in the third section of this article)
{"str": "strVal","number": 88,"bool": true,"null": null,"object": {"key1": "Val1","key2": "Val2","key3": "Val3"},"array": ["elem1","elem2","elem3","elem4"],"combination": [{"name": "Google","url": "http://www.google.com"},{"name": "Baidu","url": "http://www.baidu.com"},{"name": "SoSo","url": "http://www.SoSo.com"}]}Second, the classes that will be used in Json reading (introduce the role of the classes used in reading in this article, personal understanding)
- QJsonParseError: used to report errors during JSON parsing; errors are stored when there is a problem with the JSON string format
- QJsonDocument: Provides a way to read and write JSON documents;
- QJsonObject: used to read the object type data and receive the QJsonObject object obtained from the QJsonDocument object
- QJsonArray: used to read array type data
Third, Json file reading
3.1 Read Json string into QByteArray
QFile file("./jsonTest.json"); //Create a QFile object and specify the json file path//Open the json file and judge (return 0 if unsuccessful)if(!file.open(QIODevice::ReadOnly))return 0;//read file contents into arrayQByteArray data(file.readAll());file.close(); //Close the file3.2 Read Json string to QJsonDocument, and use QJsonParseError to judge whether there is an error
QJsonParseError jError; //Create a QJsonParseError object//Use the fromJson function of QJsonDocument to read the json string, and pass in the QJsonParseError object to get the error valueQJsonDocument jDoc = QJsonDocument::fromJson(data, &jError);//Determine whether the error obtained by the QJsonParseError object contains errors, and returns 0 if it doesif(jError.error != QJsonParseError::NoError)return 0;3.3 Get QJsonObject and read the values of various types in the Json string
Get the QJsonObject object
QJsonObject jObj = jDoc.object();Get value of general type
//read stringQString strVal = jObj["str"].toString();//Read the value (the corresponding value is converted to the corresponding type)int numVal = jObj["number"].toInt();//read logical valuebool boolVal = jObj["bool"].toBool();//Read the null value (because there is no corresponding to type function, you can use the following type to receive or isNull() function to judge)QVariant nullVal = jObj["null"].toVariant();Get the object type and its value
//The object type needs to be stored with a new QJsonObject object, and then use the new QJsonObject to get its valueQJsonObject jObj2 = jObj["object"].toObject();//Get the value of the specified key in jObj2 (the type corresponding to the value is directly converted by the corresponding function)QString key1Val = jObj2["key1"].toString();Get array type and its values
//The object type needs to be stored using the QJsonArray objectQJsonArray jArr = jObj["array"].toArray();//Get the value of the specified subscript in jArr (the value can be received in the following way or using the object of QJsonValue)QString arr1Val = jArr.at(0).toString();Read value of composite type
//The first layer type of the combined object is an array, which needs to be stored in a QJsonArray objectQJsonArray jArr2 = jObj["combination"].toArray();//The object stored in the array is received using QJsonObjectQJsonObject jObj3 = jArr2.at(0).toObject();//Get the value of the specified key in the objectQString name = jObj3["name"].toString();Summary
It is recommended to understand the Json file format before learning, and secondly, you need to be careful in the process of writing code, similar object names are easy to confuse
Related Articles
Qt writes Json files (including source code + comments)
Qt read and write ini files (including source code + comments)
Qt reads and writes XML files (including source code + comments)
Friendly reminder - I can't understand what is private, let's make progress together
(Creating is not easy, please leave a free compliment thank youo/)
Note: The article is a summary of the problems encountered by the author during the programming process. The content is for reference only. If there are any mistakes, please point out.
Note: If there is any infringement, please contact the author to delete it
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
随机推荐
VPP snort插件
系统存储的基本管理--挂载,分区,用户配额
IDO预售DAPP系统开发(NFT挖矿)
阿里云上安装MQTT服务器
SIGIR'22 推荐系统论文之序列推荐(长文)篇
威纶通触摸屏如何隐藏系统设置箭头图标及通过参数进入系统设置?
如何利用PHP实现词法分析器与自定义语言
SQL查询数据以及排序
浅聊组合函数
Qt | 关于样式表的使用 QStyleSheet
Basic management of system storage -- mounts, partitions, user quotas
CWE4.8:2022年危害最大的25种软件安全问题
SSRF(服务器端请求伪造)
WWW'22 推荐系统论文之图神经网络篇
第十七天笔记
机械臂速成小指南(十六):带抛物线过渡的线性规划
机械臂速成小指南(十五):线性规划
SIGIR'22 推荐系统论文之POI篇
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
面试必问的HashCode技术内幕









