qt5 解析Json文件

/* test.json */
{
   "appDesc": {
      "description": "SomeDescription",
      "message": "SomeMessage"
   },
   "appName": {
      "description": "Home",
      "message": "Welcome",
      "imp":["awesome","best","good"]
   }
}


void readJson()
   {
      QString val;
      QFile file;
      file.setFileName("test.json");
      file.open(QIODevice::ReadOnly | QIODevice::Text);
      val = file.readAll();
      file.close();
      qWarning() << val;
      QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
      QJsonObject sett2 = d.object();
      QJsonValue value = sett2.value(QString("appName"));
      qWarning() << value;
      QJsonObject item = value.toObject();
      qWarning() << tr("QJsonObject of description: ") << item;

      /* incase of string value get value and convert into string*/
      qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];
      QJsonValue subobj = item["description"];
      qWarning() << subobj.toString();

      /* incase of array get array and convert into string*/
      qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"];
      QJsonArray test = item["imp"].toArray();
      qWarning() << test[1].toString();
   }


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


摘于上面的链接,大部分已经能用了。


我来说下其他情况:

{"file":"book.png","frames":{
"v1":{"x":1,"y":91,"w":68,"h":87,"offX":0,"offY":0,"sourceW":68,"sourceH":87},
"v2":{"x":1,"y":1,"w":68,"h":88,"offX":0,"offY":0,"sourceW":68,"sourceH":88},
"v3":{"x":209,"y":1,"w":66,"h":87,"offX":0,"offY":0,"sourceW":66,"sourceH":87},
"v4":{"x":71,"y":1,"w":67,"h":88,"offX":0,"offY":0,"sourceW":67,"sourceH":88},
"v5":{"x":71,"y":91,"w":67,"h":88,"offX":0,"offY":0,"sourceW":67,"sourceH":88},
"v6":{"x":140,"y":1,"w":67,"h":87,"offX":0,"offY":0,"sourceW":67,"sourceH":87},
"v7":{"x":140,"y":90,"w":67,"h":87,"offX":0,"offY":0,"sourceW":67,"sourceH":87}}}

像这样的json,想要得到frames里所有的内容,因为它不是一个数组,所以要用迭代器来访问,类似这样的代码:


bool MainWindow::parseJsonFile(){

    QString val;
    QFile file;
    file.setFileName("test.json");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    val = file.readAll();
    file.close();
    qWarning() << val;
    QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
    QJsonObject rootObject = d.object();
    QJsonValue pngNameJsonValue = rootObject.value(QString("file"));
    qWarning() << pngNameJsonValue.toString();

    QJsonValue framesJsonValue = rootObject.value(QString("frames"));
    qWarning() << framesJsonValue;

    QStringList imgNameList = framesJsonValue.toObject().keys();
    QJsonObject frameObject = framesJsonValue.toObject();
    int index = 0;
    for(auto beginItr = frameObject.begin(); beginItr != frameObject.end(); ++beginItr){

       QJsonValue eachImageJsonValue = *beginItr;

       QJsonObject eachImageJsonObject = eachImageJsonValue.toObject();
     
       //eachImageJsonObject["x"], eachImageJsonObject["y"] ...
    }
    return true;
}

还有QJsonValue里用.keys()得到所有的key,然后就可以通过["key"] 来访问了。

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

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓦力冫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值