Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Error when reading Json File

    General and Desktop
    qt5 json jsonparser
    6
    10
    732
    Loading More Posts
    • 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
      deleted286 last edited by

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

      }

      jsulm Pablo J. Rogina 2 Replies Last reply Reply Quote -1
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

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

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 0
        • mranger90
          mranger90 last edited by

          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 Reply Quote 1
          • D
            deleted286 @mranger90 last edited by

            @mranger90 thank you. My json was wrong

            1 Reply Last reply Reply Quote 0
            • jsulm
              jsulm Lifetime Qt Champion @deleted286 last edited by 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...

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

              D 1 Reply Last reply Reply Quote 4
              • D
                deleted286 @jsulm last edited by

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

                JonB 1 Reply Last reply Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion last edited by Christian Ehrlicher

                  Still no code tags...

                  Qt has to stay free or it will die.

                  D 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @deleted286 last edited by JonB

                    @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 Reply Quote 3
                    • Pablo J. Rogina
                      Pablo J. Rogina @deleted286 last edited by Pablo J. Rogina

                      @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 Reply Quote 4
                      • D
                        deleted286 @Christian Ehrlicher last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post