qt5 解析Json文件

[cpp] view plain copy
  1. /* test.json */  
  2. {  
  3.    "appDesc": {  
  4.       "description""SomeDescription",  
  5.       "message""SomeMessage"  
  6.    },  
  7.    "appName": {  
  8.       "description""Home",  
  9.       "message""Welcome",  
  10.       "imp":["awesome","best","good"]  
  11.    }  
  12. }  
  13.   
  14.   
  15. void readJson()  
  16.    {  
  17.       QString val;  
  18.       QFile file;  
  19.       file.setFileName("test.json");  
  20.       file.open(QIODevice::ReadOnly | QIODevice::Text);  
  21.       val = file.readAll();  
  22.       file.close();  
  23.       qWarning() << val;  
  24.       QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());  
  25.       QJsonObject sett2 = d.object();  
  26.       QJsonValue value = sett2.value(QString("appName"));  
  27.       qWarning() << value;  
  28.       QJsonObject item = value.toObject();  
  29.       qWarning() << tr("QJsonObject of description: ") << item;  
  30.   
  31.       /* incase of string value get value and convert into string*/  
  32.       qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];  
  33.       QJsonValue subobj = item["description"];  
  34.       qWarning() << subobj.toString();  
  35.   
  36.       /* incase of array get array and convert into string*/  
  37.       qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"];  
  38.       QJsonArray test = item["imp"].toArray();  
  39.       qWarning() << test[1].toString();  
  40.    }  


http://stackoverflow.com/questions/15893040/how-to-create-read-write-json-files-in-qt5


摘於上面的鏈接,大部分已經能用了。


我來說下其他情況:

[javascript] view plain copy
  1. {"file":"book.png","frames":{  
  2. "v1":{"x":1,"y":91,"w":68,"h":87,"offX":0,"offY":0,"sourceW":68,"sourceH":87},  
  3. "v2":{"x":1,"y":1,"w":68,"h":88,"offX":0,"offY":0,"sourceW":68,"sourceH":88},  
  4. "v3":{"x":209,"y":1,"w":66,"h":87,"offX":0,"offY":0,"sourceW":66,"sourceH":87},  
  5. "v4":{"x":71,"y":1,"w":67,"h":88,"offX":0,"offY":0,"sourceW":67,"sourceH":88},  
  6. "v5":{"x":71,"y":91,"w":67,"h":88,"offX":0,"offY":0,"sourceW":67,"sourceH":88},  
  7. "v6":{"x":140,"y":1,"w":67,"h":87,"offX":0,"offY":0,"sourceW":67,"sourceH":87},  
  8. "v7":{"x":140,"y":90,"w":67,"h":87,"offX":0,"offY":0,"sourceW":67,"sourceH":87}}}  

像這樣的json,想要得到frames裏所有的內容,因爲它不是一個數組,所以要用迭代器來訪問,類似這樣的代碼:


[cpp] view plain copy
  1. bool MainWindow::parseJsonFile(){  
  2.   
  3.     QString val;  
  4.     QFile file;  
  5.     file.setFileName("test.json");  
  6.     file.open(QIODevice::ReadOnly | QIODevice::Text);  
  7.     val = file.readAll();  
  8.     file.close();  
  9.     qWarning() << val;  
  10.     QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());  
  11.     QJsonObject rootObject = d.object();  
  12.     QJsonValue pngNameJsonValue = rootObject.value(QString("file"));  
  13.     qWarning() << pngNameJsonValue.toString();  
  14.   
  15.     QJsonValue framesJsonValue = rootObject.value(QString("frames"));  
  16.     qWarning() << framesJsonValue;  
  17.   
  18.     QStringList imgNameList = framesJsonValue.toObject().keys();  
  19.     QJsonObject frameObject = framesJsonValue.toObject();  
  20.     int index = 0;  
  21.     for(auto beginItr = frameObject.begin(); beginItr != frameObject.end(); ++beginItr){  
  22.   
  23.        QJsonValue eachImageJsonValue = *beginItr;  
  24.   
  25.        QJsonObject eachImageJsonObject = eachImageJsonValue.toObject();  
  26.        
  27.        //eachImageJsonObject["x"], eachImageJsonObject["y"] ...  
  28.     }  
  29.     return true;  
  30. }  

還有QJsonValue裏用.keys()得到所有的key,然後就可以通過["key"] 來訪問了。

http://www.waitingfy.com/archives/1775


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章