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
QtWS25 Last Chance

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 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();
        }
    
      }
    

    }

    jsulmJ Pablo J. RoginaP 2 Replies Last reply
    -1
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      • mranger90M Offline
        mranger90M Offline
        mranger90
        wrote on 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
        1
        • mranger90M mranger90

          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 last edited by
          #4

          @mranger90 thank you. My json was wrong

          1 Reply Last reply
          0
          • D deleted286

            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();
                }
            
              }
            

            }

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on 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
            4
            • jsulmJ jsulm

              @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 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
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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
                0
                • D deleted286

                  @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 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

                    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();
                        }
                    
                      }
                    

                    }

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on 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
                    • Christian EhrlicherC Christian Ehrlicher

                      Still no code tags...

                      D Offline
                      D Offline
                      deleted286
                      wrote on last edited by
                      #10

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

                      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