Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why QJsonArray adding index on each entry in file?



  • 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


  • Lifetime Qt Champion

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



  • 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
      }
     ]
    }
    

  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    So is it working as expected ?



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



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


  • Thanks, @VRonin, it works like charm :)


  • Lifetime Qt Champion

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


Log in to reply