当前位置:网站首页>QT implements JSON parsing

QT implements JSON parsing

2022-07-05 10:24:00 InfoQ



Premise points

json file , You can store the data you need by key value , Bin also supports multiple types of storage , Similar to a structured database , In the reading json Documents can be accurately obtained through the corresponding keywords . It is a tree structure , We can set the number of leaves and the meaning it represents , Designate the father of each leaf , And father's father ......

1.  First get the address   Read json file

QFile file(QString::fromStdString(json));
bool bOpen = file.open(QIODevice::ReadOnly);
 if (bOpen == false)
 {
 return item;
 }
 QByteArray data = file.readAll();
file.close();

Read json file   Then you must know his position , Reason for passing the location QFile library   Open the file
QIODevice::ReadOnly
Set the file to read only mode , If set to readable and writable , In the later processing data , In case the document is modified halfway .

2.#  Translate data into json Can be read

1.  First
QJsonDocument doc = QJsonDocument::fromJson(data);// Reading and writing json file
if (!doc.isObject())
 {
 return item;
 }

utilize Qt Bring your own library   obtain json Content  
QJsonDocument::fromJson(data)
  Determine whether it is a reference type
2. Display the content corresponding to the keyword
QStringList keys = obj.keys();
for(int i=0;i<keys.size();i++)
 {
 qDebug() << &quot;key&quot; << i << &quot; is:&quot; << keys.at(i);
 }

This method is to obtain the content of keywords through the location of keywords , Next, we will use another method : Use keywords to read the corresponding content .

3.json Different ways of reading files , And his type

1. The first one is json Format
json file   Content :

{
 &quot;optionA&quot;: &quot;aaa&quot;,
 &quot;optionB&quot;: &quot;bbbb&quot;,
 &quot;score&quot;: 3
}

Parsing code :

QJsonObject obj = doc.object();// encapsulation json object
item.content = obj[&quot;content&quot;].toString().toStdString();
item.optionA = obj[&quot;optionA&quot;].toString().toStdString();
item.optionB = obj[&quot;optionB&quot;].toString().toStdString();
item.score = obj[&quot;score&quot;].toInt();//int  type   direct toint  Don't convert to string

The above examples can be found ,json It is mainly composed of keywords and corresponding values , Bin gei has other types , It can be parsed directly .QJsonObject Is to encapsulate a json object , Through this object , Use the above method to get the content of keywords .( notes :[&quot;content&quot;] The corresponding content must be with json The keywords in the file are the same , Otherwise, the resolution succeeds )
2. The second kind json Format
json file   Content :

&quot;questinList&quot;: [{
 &quot;optionA&quot;: &quot;aaa&quot;,
 &quot;optionB&quot;: &quot;bbbb&quot;,
 }, {
 &quot;optionA&quot;: &quot;aaa&quot;,
 &quot;optionB&quot;: &quot;bbbb&quot;,
 }]

Parsing code :

QJsonArray questinlist = root[&quot;questinlist&quot;].toArray();
for(int i = 0; i < questinlist.count(); i++)
{
QJsonObject obj = questinlist.at(i).toObject();
item.content = obj[&quot;content&quot;].toString().toStdString();
item.optionA = obj[&quot;optionA&quot;].toString().toStdString();
item.optionB = obj[&quot;optionB&quot;].toString().toStdString();
data.questinList.push_back(item);
 }

It can be done by json File discovery  questinList It's a list  It internally stores many data groups of the same type , It's like a mother and her children , His children all have the same genes , That is, they all have the same keywords , But everyone doesn't understand , Therefore, the data corresponding to their keywords may not be all the same . A mother is a Array, By getting the number of mothers and children , Bring every child out , Then move to another house where they are stored .

This is the end of today's update !  I hope you guys can criticize and give advice !
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051000464342.html