qt5 解析Json文件

  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;  
posted @ 2018-01-30 14:09  苍月代表我  阅读(292)  评论(0编辑  收藏  举报