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. Error when reading Json File

Error when reading Json File

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5jsonjsonparser
10 Posts 6 Posters 2.0k 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
    deleted286
    wrote on 27 Jan 2021, 14:27 last edited by
    #1

    Hi everyone. I want to read a json file. It gives me an error in there:
    How can i fix it

     ```
    

    if(json_error.error != QJsonParseError::NoError)
    {
    qDebug() << "json error!";
    return 1;
    }

    
    
    
    

    #include <QFile>
    #include <QDebug>
    #include <QJsonParseError>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QJsonArray>

    int main()
    {

    QFile loadFile("/home/a/QT Projects/JsonDeneme2/fruit.json");
    
     if(!loadFile.open(QIODevice::ReadOnly))
     {
         qDebug() << "could't open projects json";
         return 1;
     }
    
     QByteArray allData = loadFile.readAll();
     loadFile.close();
    
     QJsonParseError json_error;
     QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
    
     if(json_error.error != QJsonParseError::NoError)
     {
         qDebug() << "json error!";
         return 1;
     }
    
     QJsonObject rootObj = jsonDoc.object();
    
     QStringList keys = rootObj.keys();
     for(int i = 0; i < keys.size(); i++)
     {
         qDebug() << "key" << i << " is:" << keys.at(i);
     }
    
          //Because it is a predefined JSON data format, it can be read like this here.
     if(rootObj.contains("first fruit"))
     {
        QJsonObject subObj = rootObj.value("first fruit").toObject();
        qDebug() << "describe is:" << subObj["describe"].toString();
        qDebug() << "icon is:" << subObj["icon"].toString();
        qDebug() << "name is:" << subObj["name"].toString();
     }
    
     if(rootObj.contains("second fruit"))
     {
        QJsonObject subObj = rootObj.value("second fruit").toObject();
        qDebug() << "describe is:" << subObj.value("describe").toString();
        qDebug() << "icon is:" << subObj.value("icon").toString();
        qDebug() << "name is:" << subObj.value("name").toString();
     }
    
     if(rootObj.contains("third fruit array"))
     {
        QJsonArray subArray = rootObj.value("three fruit array").toArray();
        for(int i = 0; i< subArray.size(); i++)
        {
            qDebug() << i<<" value is:" << subArray.at(i).toString();
        }
    
      }
    

    }

    J P 2 Replies Last reply 27 Jan 2021, 14:37
    -1
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 27 Jan 2021, 14:28 last edited by
      #2

      Don't know how often we must tell you to use code tags to make your code readable!

      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
      0
      • M Offline
        M Offline
        mranger90
        wrote on 27 Jan 2021, 14:30 last edited by
        #3

        You should make sure it really is valid json. I'd run the file through jsonlint.com and it will tell you where the error is.

        D 1 Reply Last reply 27 Jan 2021, 14:37
        1
        • M mranger90
          27 Jan 2021, 14:30

          You should make sure it really is valid json. I'd run the file through jsonlint.com and it will tell you where the error is.

          D Offline
          D Offline
          deleted286
          wrote on 27 Jan 2021, 14:37 last edited by
          #4

          @mranger90 thank you. My json was wrong

          1 Reply Last reply
          0
          • D deleted286
            27 Jan 2021, 14:27

            Hi everyone. I want to read a json file. It gives me an error in there:
            How can i fix it

             ```
            

            if(json_error.error != QJsonParseError::NoError)
            {
            qDebug() << "json error!";
            return 1;
            }

            
            
            
            

            #include <QFile>
            #include <QDebug>
            #include <QJsonParseError>
            #include <QJsonDocument>
            #include <QJsonObject>
            #include <QJsonArray>

            int main()
            {

            QFile loadFile("/home/a/QT Projects/JsonDeneme2/fruit.json");
            
             if(!loadFile.open(QIODevice::ReadOnly))
             {
                 qDebug() << "could't open projects json";
                 return 1;
             }
            
             QByteArray allData = loadFile.readAll();
             loadFile.close();
            
             QJsonParseError json_error;
             QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
            
             if(json_error.error != QJsonParseError::NoError)
             {
                 qDebug() << "json error!";
                 return 1;
             }
            
             QJsonObject rootObj = jsonDoc.object();
            
             QStringList keys = rootObj.keys();
             for(int i = 0; i < keys.size(); i++)
             {
                 qDebug() << "key" << i << " is:" << keys.at(i);
             }
            
                  //Because it is a predefined JSON data format, it can be read like this here.
             if(rootObj.contains("first fruit"))
             {
                QJsonObject subObj = rootObj.value("first fruit").toObject();
                qDebug() << "describe is:" << subObj["describe"].toString();
                qDebug() << "icon is:" << subObj["icon"].toString();
                qDebug() << "name is:" << subObj["name"].toString();
             }
            
             if(rootObj.contains("second fruit"))
             {
                QJsonObject subObj = rootObj.value("second fruit").toObject();
                qDebug() << "describe is:" << subObj.value("describe").toString();
                qDebug() << "icon is:" << subObj.value("icon").toString();
                qDebug() << "name is:" << subObj.value("name").toString();
             }
            
             if(rootObj.contains("third fruit array"))
             {
                QJsonArray subArray = rootObj.value("three fruit array").toArray();
                for(int i = 0; i< subArray.size(); i++)
                {
                    qDebug() << i<<" value is:" << subArray.at(i).toString();
                }
            
              }
            

            }

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 27 Jan 2021, 14:37 last edited by jsulm
            #5

            @suslucoder If you want to get help then provide needed information: you did not post the actuall error message and you did not post an example of your JSON file you're trying to parse! And you really expect to get help? I will simply ignore such posts in the future...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply 27 Jan 2021, 15:15
            4
            • J jsulm
              27 Jan 2021, 14:37

              @suslucoder If you want to get help then provide needed information: you did not post the actuall error message and you did not post an example of your JSON file you're trying to parse! And you really expect to get help? I will simply ignore such posts in the future...

              D Offline
              D Offline
              deleted286
              wrote on 27 Jan 2021, 15:15 last edited by
              #6

              @jsulm Because i didnt get any error message. I know is there a mistake because i've added this part to my code.

              if(json_error.error != QJsonParseError::NoError)
              {
              qDebug() << "json error!";
              return 1;
              }
              

              I thought, I was wrong the parsing part. If i think that my JSON is wrong, i would share it.

              JonBJ 1 Reply Last reply 27 Jan 2021, 15:29
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 27 Jan 2021, 15:16 last edited by Christian Ehrlicher
                #7

                Still no code tags...

                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 28 Jan 2021, 06:00
                0
                • D deleted286
                  27 Jan 2021, 15:15

                  @jsulm Because i didnt get any error message. I know is there a mistake because i've added this part to my code.

                  if(json_error.error != QJsonParseError::NoError)
                  {
                  qDebug() << "json error!";
                  return 1;
                  }
                  

                  I thought, I was wrong the parsing part. If i think that my JSON is wrong, i would share it.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 27 Jan 2021, 15:29 last edited by JonB
                  #8

                  @suslucoder said in Error when reading Json File:

                  qDebug() << "json error!";

                  I suggest you look at the docs of QJsonParseError Struct and use that in your code to extract much more meaningful information than this!

                  1 Reply Last reply
                  3
                  • D deleted286
                    27 Jan 2021, 14:27

                    Hi everyone. I want to read a json file. It gives me an error in there:
                    How can i fix it

                     ```
                    

                    if(json_error.error != QJsonParseError::NoError)
                    {
                    qDebug() << "json error!";
                    return 1;
                    }

                    
                    
                    
                    

                    #include <QFile>
                    #include <QDebug>
                    #include <QJsonParseError>
                    #include <QJsonDocument>
                    #include <QJsonObject>
                    #include <QJsonArray>

                    int main()
                    {

                    QFile loadFile("/home/a/QT Projects/JsonDeneme2/fruit.json");
                    
                     if(!loadFile.open(QIODevice::ReadOnly))
                     {
                         qDebug() << "could't open projects json";
                         return 1;
                     }
                    
                     QByteArray allData = loadFile.readAll();
                     loadFile.close();
                    
                     QJsonParseError json_error;
                     QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
                    
                     if(json_error.error != QJsonParseError::NoError)
                     {
                         qDebug() << "json error!";
                         return 1;
                     }
                    
                     QJsonObject rootObj = jsonDoc.object();
                    
                     QStringList keys = rootObj.keys();
                     for(int i = 0; i < keys.size(); i++)
                     {
                         qDebug() << "key" << i << " is:" << keys.at(i);
                     }
                    
                          //Because it is a predefined JSON data format, it can be read like this here.
                     if(rootObj.contains("first fruit"))
                     {
                        QJsonObject subObj = rootObj.value("first fruit").toObject();
                        qDebug() << "describe is:" << subObj["describe"].toString();
                        qDebug() << "icon is:" << subObj["icon"].toString();
                        qDebug() << "name is:" << subObj["name"].toString();
                     }
                    
                     if(rootObj.contains("second fruit"))
                     {
                        QJsonObject subObj = rootObj.value("second fruit").toObject();
                        qDebug() << "describe is:" << subObj.value("describe").toString();
                        qDebug() << "icon is:" << subObj.value("icon").toString();
                        qDebug() << "name is:" << subObj.value("name").toString();
                     }
                    
                     if(rootObj.contains("third fruit array"))
                     {
                        QJsonArray subArray = rootObj.value("three fruit array").toArray();
                        for(int i = 0; i< subArray.size(); i++)
                        {
                            qDebug() << i<<" value is:" << subArray.at(i).toString();
                        }
                    
                      }
                    

                    }

                    P Offline
                    P Offline
                    Pablo J. Rogina
                    wrote on 27 Jan 2021, 17:34 last edited by Pablo J. Rogina
                    #9

                    @suslucoder said in Error when reading Json File:
                    In addition to all the previous requests and suggestions, you may also want to learn about the capabilities of the Qt framework, and its different classes.

                    And by reading the documentation of QJsonParseError you'll see that error is a enumeration that will provide the type of error while parsing JSON.

                    Moreover, QJsonParseError::errorString() method will provide "the human-readable message appropriate to the reported JSON parsing error"

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    4
                    • C Christian Ehrlicher
                      27 Jan 2021, 15:16

                      Still no code tags...

                      D Offline
                      D Offline
                      deleted286
                      wrote on 28 Jan 2021, 06:00 last edited by
                      #10

                      @Christian-Ehrlicher I did use it. It looks in a code tag in my screen.

                      1 Reply Last reply
                      0

                      1/10

                      27 Jan 2021, 14:27

                      • Login

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