Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-22289

QJsonArray转换出错

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Not Evaluated
    • None
    • Qt Creator 4.8.2
    • C/C++/Obj-C++ Support
    • None
    • (Qt5.12.2+QtCreator 4.8.2)
    • Windows

    Description

          今天(2019.4.12),我在使用QT解析Json时出现错误。

      我尝试使用Qt自带的Json类进行解析w.json,参考文档为:[

      Qt5.9解析json文件详细用法示例

          但是我再尝试将QJsonValue使用toArray()转换为数组时,出现error: invalid use of incomplete type 'class QJsonArray'
      jsonVal.toArray();
      ^

          我在stackflow中看到,出现这样问题的原因是由于类对象创建出现在类定义之前,我猜测这个或许是一个bug,如果不是的话,抱歉打扰您了。

       

      我的代码如下:

      #include <QCoreApplication>#include <QJsonDocument>#include <QString>#include <QFile>#include <QDebug>#include <QByteArray>#include <QJsonObject>#include <QJsonParseError>#include <typeinfoint main(int argc, char *argv[]){ QCoreApplication a(argc, argv);// 打开Json文件 QFile file("E:\\Project\\Qt\\QJson
      w.json"
      ); file.open(QIODevice::ReadOnly);//创建QJsonDoc对象 QJsonParseError json_error; QJsonDocument json_doc=QJsonDocument::fromJson(file.readAll(),&json_error); if(json_error.error!=QJsonParseError::NoError) qDebug()<<json_error.error// 读取Json数据 QJsonObject jsonObj=json_doc.object(); qDebug()<<jsonObj.value("cityid").toString();//获取一级键值  QJsonObject jsonObj_data=jsonObj.value("data").toObject();//获取二级键值(是一个数组) QJsonValue jsonVal=jsonObj.value("data"); if(jsonVal.isArray()){ qDebug()<<"data is a array"; jsonVal.toArray();  }  return a.exec();} 

       

      也许应该使用IDE整理一下代码?Clion整理代码的快捷键应该是Ctrl+shift+L?

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kosjar Nikolai Kosjar
            dragontao 龙涛 张
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes