QT read json keyvalue pairs in a array











up vote
0
down vote

favorite












Following JSON should be read in a qt, and stored in an array as key value pairs, so I can work with these values:



json:



    {
"datasources":[
{"id":1,"name":"TPS Position"},
{"id":2,"name":"TPS Timer"},
{"id":3,"name":"Lateral G"},
{"id":4,"name":"Longitudal G"},
{"id":5,"name":"Z"},
{"id":6,"name":"AFR"}
]}


I read the QT JSON savegame example, but I can't figure out how to read Key values.
This is the qt code that I have, but I don't know how to go on:



    jsonFileValue = datasourcesjson.readAll();
datasourcesjson.close();

QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;


This is the output:



QJsonArray([{"id":1,"name":"TPS Position"},{"id":2,"name":"TPS Timer"},{"id":3,"name":"Lateral G"},{"id":4,"name":"Longitudal G"},{"id":5,"name":"Z"},{"id":6,"name":"AFR"}])


But how can I get this data in an array to work with in my programm like QMap for key values?










share|improve this question




















  • 1




    Just read the documentation! - it's really not that hard guys...
    – Felix
    Nov 17 at 11:08















up vote
0
down vote

favorite












Following JSON should be read in a qt, and stored in an array as key value pairs, so I can work with these values:



json:



    {
"datasources":[
{"id":1,"name":"TPS Position"},
{"id":2,"name":"TPS Timer"},
{"id":3,"name":"Lateral G"},
{"id":4,"name":"Longitudal G"},
{"id":5,"name":"Z"},
{"id":6,"name":"AFR"}
]}


I read the QT JSON savegame example, but I can't figure out how to read Key values.
This is the qt code that I have, but I don't know how to go on:



    jsonFileValue = datasourcesjson.readAll();
datasourcesjson.close();

QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;


This is the output:



QJsonArray([{"id":1,"name":"TPS Position"},{"id":2,"name":"TPS Timer"},{"id":3,"name":"Lateral G"},{"id":4,"name":"Longitudal G"},{"id":5,"name":"Z"},{"id":6,"name":"AFR"}])


But how can I get this data in an array to work with in my programm like QMap for key values?










share|improve this question




















  • 1




    Just read the documentation! - it's really not that hard guys...
    – Felix
    Nov 17 at 11:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Following JSON should be read in a qt, and stored in an array as key value pairs, so I can work with these values:



json:



    {
"datasources":[
{"id":1,"name":"TPS Position"},
{"id":2,"name":"TPS Timer"},
{"id":3,"name":"Lateral G"},
{"id":4,"name":"Longitudal G"},
{"id":5,"name":"Z"},
{"id":6,"name":"AFR"}
]}


I read the QT JSON savegame example, but I can't figure out how to read Key values.
This is the qt code that I have, but I don't know how to go on:



    jsonFileValue = datasourcesjson.readAll();
datasourcesjson.close();

QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;


This is the output:



QJsonArray([{"id":1,"name":"TPS Position"},{"id":2,"name":"TPS Timer"},{"id":3,"name":"Lateral G"},{"id":4,"name":"Longitudal G"},{"id":5,"name":"Z"},{"id":6,"name":"AFR"}])


But how can I get this data in an array to work with in my programm like QMap for key values?










share|improve this question















Following JSON should be read in a qt, and stored in an array as key value pairs, so I can work with these values:



json:



    {
"datasources":[
{"id":1,"name":"TPS Position"},
{"id":2,"name":"TPS Timer"},
{"id":3,"name":"Lateral G"},
{"id":4,"name":"Longitudal G"},
{"id":5,"name":"Z"},
{"id":6,"name":"AFR"}
]}


I read the QT JSON savegame example, but I can't figure out how to read Key values.
This is the qt code that I have, but I don't know how to go on:



    jsonFileValue = datasourcesjson.readAll();
datasourcesjson.close();

QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;


This is the output:



QJsonArray([{"id":1,"name":"TPS Position"},{"id":2,"name":"TPS Timer"},{"id":3,"name":"Lateral G"},{"id":4,"name":"Longitudal G"},{"id":5,"name":"Z"},{"id":6,"name":"AFR"}])


But how can I get this data in an array to work with in my programm like QMap for key values?







json qt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 11:44

























asked Nov 17 at 10:52









Basti An

6514




6514








  • 1




    Just read the documentation! - it's really not that hard guys...
    – Felix
    Nov 17 at 11:08














  • 1




    Just read the documentation! - it's really not that hard guys...
    – Felix
    Nov 17 at 11:08








1




1




Just read the documentation! - it's really not that hard guys...
– Felix
Nov 17 at 11:08




Just read the documentation! - it's really not that hard guys...
– Felix
Nov 17 at 11:08












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.



    QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;

for (const auto m_DataSource : m_DataSourceArray)
// or instead of c++11 for-loop, you can use foreach, like:
// foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
{
QJsonObject m_DataSourceObject = m_DataSource.toObject();
qWarning() << "Id: "
<< m_DataSourceObject.value("id").toInt()
<< " and name: "
<< m_DataSourceObject.value("name").toString();
}


Outputs:



Id:  1  and name:  "TPS Position"
Id: 2 and name: "TPS Timer"
Id: 3 and name: "Lateral G"
Id: 4 and name: "Longitudal G"
Id: 5 and name: "Z"
Id: 6 and name: "AFR"


If you don't want to loop through all members, you can also read a single value from array.



Following code prints 'name' value of the second member:



    qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();


Outputs:



"TPS Timer"


Hope this helps.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350502%2fqt-read-json-keyvalue-pairs-in-a-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.



        QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

    QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

    QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
    qWarning() << m_DataSourceArray;

    for (const auto m_DataSource : m_DataSourceArray)
    // or instead of c++11 for-loop, you can use foreach, like:
    // foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
    {
    QJsonObject m_DataSourceObject = m_DataSource.toObject();
    qWarning() << "Id: "
    << m_DataSourceObject.value("id").toInt()
    << " and name: "
    << m_DataSourceObject.value("name").toString();
    }


    Outputs:



    Id:  1  and name:  "TPS Position"
    Id: 2 and name: "TPS Timer"
    Id: 3 and name: "Lateral G"
    Id: 4 and name: "Longitudal G"
    Id: 5 and name: "Z"
    Id: 6 and name: "AFR"


    If you don't want to loop through all members, you can also read a single value from array.



    Following code prints 'name' value of the second member:



        qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();


    Outputs:



    "TPS Timer"


    Hope this helps.






    share|improve this answer



























      up vote
      0
      down vote













      Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.



          QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

      QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

      QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
      qWarning() << m_DataSourceArray;

      for (const auto m_DataSource : m_DataSourceArray)
      // or instead of c++11 for-loop, you can use foreach, like:
      // foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
      {
      QJsonObject m_DataSourceObject = m_DataSource.toObject();
      qWarning() << "Id: "
      << m_DataSourceObject.value("id").toInt()
      << " and name: "
      << m_DataSourceObject.value("name").toString();
      }


      Outputs:



      Id:  1  and name:  "TPS Position"
      Id: 2 and name: "TPS Timer"
      Id: 3 and name: "Lateral G"
      Id: 4 and name: "Longitudal G"
      Id: 5 and name: "Z"
      Id: 6 and name: "AFR"


      If you don't want to loop through all members, you can also read a single value from array.



      Following code prints 'name' value of the second member:



          qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();


      Outputs:



      "TPS Timer"


      Hope this helps.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.



            QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

        QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

        QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
        qWarning() << m_DataSourceArray;

        for (const auto m_DataSource : m_DataSourceArray)
        // or instead of c++11 for-loop, you can use foreach, like:
        // foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
        {
        QJsonObject m_DataSourceObject = m_DataSource.toObject();
        qWarning() << "Id: "
        << m_DataSourceObject.value("id").toInt()
        << " and name: "
        << m_DataSourceObject.value("name").toString();
        }


        Outputs:



        Id:  1  and name:  "TPS Position"
        Id: 2 and name: "TPS Timer"
        Id: 3 and name: "Lateral G"
        Id: 4 and name: "Longitudal G"
        Id: 5 and name: "Z"
        Id: 6 and name: "AFR"


        If you don't want to loop through all members, you can also read a single value from array.



        Following code prints 'name' value of the second member:



            qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();


        Outputs:



        "TPS Timer"


        Hope this helps.






        share|improve this answer














        Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.



            QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());

        QJsonObject m_DataSourceObject = m_DataSourceDocument.object();

        QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
        qWarning() << m_DataSourceArray;

        for (const auto m_DataSource : m_DataSourceArray)
        // or instead of c++11 for-loop, you can use foreach, like:
        // foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
        {
        QJsonObject m_DataSourceObject = m_DataSource.toObject();
        qWarning() << "Id: "
        << m_DataSourceObject.value("id").toInt()
        << " and name: "
        << m_DataSourceObject.value("name").toString();
        }


        Outputs:



        Id:  1  and name:  "TPS Position"
        Id: 2 and name: "TPS Timer"
        Id: 3 and name: "Lateral G"
        Id: 4 and name: "Longitudal G"
        Id: 5 and name: "Z"
        Id: 6 and name: "AFR"


        If you don't want to loop through all members, you can also read a single value from array.



        Following code prints 'name' value of the second member:



            qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();


        Outputs:



        "TPS Timer"


        Hope this helps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 at 13:16

























        answered Nov 18 at 11:29









        Rob

        868




        868






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350502%2fqt-read-json-keyvalue-pairs-in-a-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Paul Cézanne

            UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

            Angular material date-picker (MatDatepicker) auto completes the date on focus out