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. Read Json file
Forum Updated to NodeBB v4.3 + New Features

Read Json file

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 444 Views 1 Watching
  • 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.
  • Zer0Z Offline
    Zer0Z Offline
    Zer0
    wrote on last edited by Zer0
    #1

    i have tried to read a JSON file, but it does not work properly (I'm propbably doing something wrong here).

    Note: file.open functions returns true, so the path is correct.

    PROBLEM -> it returns no data : jsonData is empty and array is empty

    here is my json

    {
        "notes": [{
            "title": "title1",
            "content": "content1"
        }, {
            "title": "title2",
            "content": "content2"
        }]
    }
    

    here is my function

    QJsonArray NotesManager::getNotesFromJson() const{
        QFile file("notes.json");
        file.open(QIODevice::ReadOnly | QIODevice::Text); //returns true
        QByteArray jsonData = file.readAll(); //jsonData is empty
        file.close();
    
        QJsonDocument document = QJsonDocument::fromJson(jsonData);
        QJsonObject object = document.object();
    
        QJsonValue value = object.value("notes");
        return value.toArray(); // returns array with no elements
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • Zer0Z Zer0

      i have tried to read a JSON file, but it does not work properly (I'm propbably doing something wrong here).

      Note: file.open functions returns true, so the path is correct.

      PROBLEM -> it returns no data : jsonData is empty and array is empty

      here is my json

      {
          "notes": [{
              "title": "title1",
              "content": "content1"
          }, {
              "title": "title2",
              "content": "content2"
          }]
      }
      

      here is my function

      QJsonArray NotesManager::getNotesFromJson() const{
          QFile file("notes.json");
          file.open(QIODevice::ReadOnly | QIODevice::Text); //returns true
          QByteArray jsonData = file.readAll(); //jsonData is empty
          file.close();
      
          QJsonDocument document = QJsonDocument::fromJson(jsonData);
          QJsonObject object = document.object();
      
          QJsonValue value = object.value("notes");
          return value.toArray(); // returns array with no elements
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @zer0 and welcome

      You didn't check, if the file exists before attempting to open it. If the file does not exist, that calling open will create it. IIRC

      SO your path is most likely wrong


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      5
      • Zer0Z Offline
        Zer0Z Offline
        Zer0
        wrote on last edited by
        #3

        thanks, you are right.

        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