Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. parsing json file
QtWS25 Last Chance

parsing json file

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 523 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dijkstra
    wrote on last edited by
    #1

    so i am trying to parse this json file

    {
        "file": "/uploads/plugin/auto/cv/libViBELPRPlugin.so",
        "number_instants" : 5,          
         "name": "ViBE-Auto",
         "JSON_File" :  "/uploads/plugin/auto/cv/libViBELPRPlugin.json",
        "gpu_servers_configurations": [
            {
                "name": "auto-Recognition-Server",
                "path": "/uploads/plugin/auto/cv/libViBELPRPlugin.so",
                "JSON_File" :  "/uploads/plugin/auto/cv/libViBELPRPlugin.json",
                 "number_instants" : 5,
            },
            {
                "name": "auto-Detection-Server",
                "path": "/uploads/plugin/auto/cv/libViBELPRPlugin.so",
                "JSON_File" :  "/uploads/plugin/auto/cv/libViBELPRPlugin.json",
                 "number_instants" : 5,
    
            }
        ]
    }
    

    using this code i am trying to print the file at the top but it doesnt print anything

    QFile file;
        file.setFileName(inputFileName);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QString file_values=file.readAll();
        file.close();
        QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8());
        QJsonObject jsonobject = parsejson.object();
        QJsonValue file_name=jsonobject.value(QString("file"));    
        cout<<file_name.toString().toStdString()<<endl;
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      "number_instants" : 5,

      You json is not valid.

      This function has a second parameter which tells you exactly where your json document is wrong.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        "number_instants" : 5,

        You json is not valid.

        This function has a second parameter which tells you exactly where your json document is wrong.

        D Offline
        D Offline
        Dijkstra
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        i added this and it prints an empty line

        if (QJsonParseError::NoError != jsonError.error) {
                cout<<file_name.toString().toStdString()<<endl;
            }
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          @Dijkstra said in parsing json file:

          i added this and it prints an empty line

          where? Please show the code. Also why do you print out the filename instead the parser error? And I already showed you a place where your json is wrong.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          D 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @Dijkstra said in parsing json file:

            i added this and it prints an empty line

            where? Please show the code. Also why do you print out the filename instead the parser error? And I already showed you a place where your json is wrong.

            D Offline
            D Offline
            Dijkstra
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            QJsonParseError jsonError;
                QFile file;
                file.setFileName(inputFileName);
                file.open(QIODevice::ReadOnly | QIODevice::Text);
                QString file_values=file.readAll();
                file.close();
                QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8(),&jsonError);
                QJsonObject jsonobject = parsejson.object();
                QJsonValue file_name=jsonobject.value(QString("file"));
                if (QJsonParseError::NoError != jsonError.error) {
                    cout<<file_name.toString().toStdString()<<endl;
                }
            

            when i tried to print the error nothing appeared

            JonBJ B 3 Replies Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              QFile::open() returns a bool which indicates if the file can be opened or not. Please check it.

              btw: Covnerting a QByteArray to a QString just to convert it back to a QByteArray two lines below is not needed. Esp. since the conversion from QByteArray to QString may kill your encoding.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              3
              • D Dijkstra

                @Christian-Ehrlicher

                QJsonParseError jsonError;
                    QFile file;
                    file.setFileName(inputFileName);
                    file.open(QIODevice::ReadOnly | QIODevice::Text);
                    QString file_values=file.readAll();
                    file.close();
                    QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8(),&jsonError);
                    QJsonObject jsonobject = parsejson.object();
                    QJsonValue file_name=jsonobject.value(QString("file"));
                    if (QJsonParseError::NoError != jsonError.error) {
                        cout<<file_name.toString().toStdString()<<endl;
                    }
                

                when i tried to print the error nothing appeared

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Dijkstra
                Additional to @Christian-Ehrlicher, you do not check the return result from QJsonDocument parsejson = QJsonDocument::fromJson(), so everything after that is useless. This is the most fundamental thing to do when reading in a JSON document.

                1 Reply Last reply
                1
                • D Dijkstra

                  @Christian-Ehrlicher

                  QJsonParseError jsonError;
                      QFile file;
                      file.setFileName(inputFileName);
                      file.open(QIODevice::ReadOnly | QIODevice::Text);
                      QString file_values=file.readAll();
                      file.close();
                      QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8(),&jsonError);
                      QJsonObject jsonobject = parsejson.object();
                      QJsonValue file_name=jsonobject.value(QString("file"));
                      if (QJsonParseError::NoError != jsonError.error) {
                          cout<<file_name.toString().toStdString()<<endl;
                      }
                  

                  when i tried to print the error nothing appeared

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  @Dijkstra said in parsing json file:

                  when i tried to print the error nothing appeared

                  No, you didn't.
                  You just printed the string value of "file", which is obviously an empty string when you get parse error.
                  So you got an empty line.

                  1 Reply Last reply
                  2
                  • D Dijkstra

                    @Christian-Ehrlicher

                    QJsonParseError jsonError;
                        QFile file;
                        file.setFileName(inputFileName);
                        file.open(QIODevice::ReadOnly | QIODevice::Text);
                        QString file_values=file.readAll();
                        file.close();
                        QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8(),&jsonError);
                        QJsonObject jsonobject = parsejson.object();
                        QJsonValue file_name=jsonobject.value(QString("file"));
                        if (QJsonParseError::NoError != jsonError.error) {
                            cout<<file_name.toString().toStdString()<<endl;
                        }
                    

                    when i tried to print the error nothing appeared

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Dijkstra
                    And additional (again!) to @Bonnie, you
                    a. Test jsonError when it may not be set to anything (read https://doc.qt.io/qt-5/qjsondocument.html#fromJson and stick to the behaviour it documents)
                    b. Most importantly, you don't test for error till after you have tried fetch further data from a call which [may have] failed, which is pointless.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved