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. Why QJsonArray adding index on each entry in file?

Why QJsonArray adding index on each entry in file?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 995 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.
  • N Offline
    N Offline
    npatil15
    wrote on last edited by
    #1

    Hi,

    I'm having a list of multiple QJsonObject, adding them to QJsonArray and then adding to QJsonDocument,

    QJsonObject recordObject;
    for(QVariantMap::const_iterator iter = m_jsonData.begin(); iter != m_jsonData.end(); ++iter)
            {
                recordObject.insert("FrameIndex", QJsonValue::fromVariant(m_frameIndex));
                recordObject.insert(iter.key(), QJsonValue::fromVariant(iter.value()));
            }
    recordsArray << recordObject;
    
    QJsonDocument jsonData;
     jsonData.setArray(recordsArray);
    

    Output:

    {
      "employees": [
        {
          "0": {
            "FrameIndex": 15,
            "s": "sunny"
          },
          "1": {
            "FrameIndex": 16,
            "s": "sunny"
          },
          "2": {
            "FrameIndex": 17,
            "s": "sunny"
          },
          "3": {
            "FrameIndex": 18,
            "s": "sunny"
          },
          "4": {
            "FrameIndex": 19,
            "s": "sunny"
          },
          "5": {
            "FrameIndex": 20,
            "s": "sunny"
          },
    "id": 1
      }
     ]
    }
    

    In the above output, I don't want that index of that each array, how I can remove it?

    Thanks

    jsulmJ 1 Reply Last reply
    0
    • N npatil15

      Hi,

      I'm having a list of multiple QJsonObject, adding them to QJsonArray and then adding to QJsonDocument,

      QJsonObject recordObject;
      for(QVariantMap::const_iterator iter = m_jsonData.begin(); iter != m_jsonData.end(); ++iter)
              {
                  recordObject.insert("FrameIndex", QJsonValue::fromVariant(m_frameIndex));
                  recordObject.insert(iter.key(), QJsonValue::fromVariant(iter.value()));
              }
      recordsArray << recordObject;
      
      QJsonDocument jsonData;
       jsonData.setArray(recordsArray);
      

      Output:

      {
        "employees": [
          {
            "0": {
              "FrameIndex": 15,
              "s": "sunny"
            },
            "1": {
              "FrameIndex": 16,
              "s": "sunny"
            },
            "2": {
              "FrameIndex": 17,
              "s": "sunny"
            },
            "3": {
              "FrameIndex": 18,
              "s": "sunny"
            },
            "4": {
              "FrameIndex": 19,
              "s": "sunny"
            },
            "5": {
              "FrameIndex": 20,
              "s": "sunny"
            },
      "id": 1
        }
       ]
      }
      

      In the above output, I don't want that index of that each array, how I can remove it?

      Thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @npatil15 said in Why QJsonArray adding index on each entry in file?:

      ow I can remove it?

      Then don't add it? You add it here:

      recordObject.insert("FrameIndex", QJsonValue::fromVariant(m_frameIndex));
      

      Remove this line.
      Or do I misunderstand something?

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

      1 Reply Last reply
      2
      • N Offline
        N Offline
        npatil15
        wrote on last edited by
        #3

        I'm looking for output like this below,

        {
          "employees": [
            {
              {
                "FrameIndex": 15,
                "s": "sunny"
              },
              {
                "FrameIndex": 16,
                "s": "sunny"
              },
              {
                "FrameIndex": 17,
                "s": "sunny"
              },
              {
                "FrameIndex": 18,
                "s": "sunny"
              },
              {
                "FrameIndex": 19,
                "s": "sunny"
              },
              {
                "FrameIndex": 20,
                "s": "sunny"
              },
        "id": 1
          }
         ]
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Your loop looks strange, m_frameIndex doesn't get incremented yet in your output it is.

          You are cumulating data in recordObject which is not an array, shouldn't it rather be something like:

          for(QVariantMap::const_iterator iter = m_jsonData.begin(); iter != m_jsonData.end(); ++iter)
          {
              QJsonObject recordObject;
              recordObject.insert("FrameIndex", QJsonValue::fromVariant(m_frameIndex));
              recordObject.insert(iter.key(), QJsonValue::fromVariant(iter.value()));
              recordsArray << recordObject;
          }
          

          ?

          Note that in this code taken from your snippet, I still don't see where m_frameIndex gets updated.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • N Offline
            N Offline
            npatil15
            wrote on last edited by npatil15
            #5

            @SGaist said in Why QJsonArray adding index on each entry in file?:

            m_frameIndex
            getFrameIndex() is the slot, which gets invoks on signal (when frameIndex update).

            void JsonClass::getFrameIndex(int index)
            {
                m_frameIndex = index;
            for(QVariantMap::const_iterator iter = m_jsonData.begin(); iter != m_jsonData.end(); ++iter)
            {
                QJsonObject recordObject;
                recordObject.insert("FrameIndex", QJsonValue::fromVariant(m_frameIndex));
                recordObject.insert(iter.key(), QJsonValue::fromVariant(iter.value()));
                recordsArray << recordObject;
            }
            }
            
            

            And I'm adding object data into recordsArray (define globally as, QJsonArray recordsArray )

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              So is it working as expected ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                npatil15
                wrote on last edited by
                #7

                No, it's not, still, it has the same output, having the index for each array element.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  QJsonDocument jsonData;
                  jsonData.setArray(recordsArray);

                  Output:

                  {
                    "employees":
                  

                  The two statements above can't both be true.

                  This works as expected and gives the output you want:

                  #include <QCoreApplication>
                  #include <QJsonObject>
                  #include <QJsonArray>
                  #include <QJsonDocument>
                  #include <QDebug>
                  
                  int main(int argc, char **argv)
                  {
                      QCoreApplication app(argc,argv);
                      QJsonArray recordsArray;
                      for(int iter = 0; iter <5; ++iter)
                      {
                          QJsonObject recordObject;
                          recordObject.insert(QStringLiteral("FrameIndex"), iter+15);
                          recordObject.insert(QStringLiteral("s"), QStringLiteral("sunny"));
                          recordsArray << recordObject;
                      }
                      QJsonObject employeesObject;
                      employeesObject[QStringLiteral("employees")]=recordsArray;
                      qDebug().noquote() << QJsonDocument(employeesObject).toJson();
                      return app.exec();
                  }
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  3
                  • N Offline
                    N Offline
                    npatil15
                    wrote on last edited by
                    #9

                    Thanks, @VRonin, it works like charm :)

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • N npatil15

                      Thanks, @VRonin, it works like charm :)

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @npatil15 Then please mark this topic as solved, thx.

                      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

                      • Login

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