Qt Forum

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

    Unsolved extract values from json result

    General and Desktop
    json parser json
    3
    5
    449
    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.
    • Qjay
      Qjay last edited by

      hey i get a respnse from an api which looks like this

      {
      	"results": [{
      		"series": [{
      			"columns": ["tagKey"],
      			"name": "EVENT",
      			"values": [
      				["alias"],
      				["name"],
      				["variabletype"]
      			]
      		}],
      		"statement_id": 0
      	}]
      }
      

      i want to get "values" values . how can i get it ?

      so far i have this

      Qstring result = "json result here"
      QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());
                  qDebug() << "doc :" << doc;
                  QJsonObject doc_obj = doc.object();
                  qDebug() << "doc_obj :" << doc_obj;
                  QJsonArray doc_array = doc_obj.value("results").toArray();
                  qDebug() << "doc_array : "<< doc_array;
      
      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        Where exactly do you hit a problem now? On how to iterate over a QJsonArray?

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 2
        • Qjay
          Qjay last edited by

          i want to get value form "values" like alias,name, variabletype.

          yes you can say i am having issue with iterating over it.

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @Qjay last edited by

            @Qjay There is https://doc.qt.io/qt-5/qjsonarray.html#operator-5b-5d-1 operator to iterate over QJsonArray elements.
            And you get number of elements using https://doc.qt.io/qt-5/qjsonarray.html#size
            So, a for() loop shouldn't be an issue.

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

            1 Reply Last reply Reply Quote 2
            • Qjay
              Qjay last edited by Qjay

              well this is how i have done it. Not sure if this is even the right way ( looks like a mess to me)

              QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());
                          qDebug() << "doc :" << doc;
                          QJsonObject doc_obj = doc.object();
                          qDebug() << "doc_obj :" << doc_obj;
                          QJsonArray doc_array = doc_obj.value("results").toArray();
                          doc_obj =  doc_array[0].toObject();
                          doc_array =  doc_obj.value("series").toArray();
                          doc_obj =  doc_array[0].toObject();
                          doc_array =  doc_obj.value("values").toArray();
                          for(int i = 0; i < doc_array.size(); i++)
                          {
                              QJsonArray arr =  doc_array[i].toArray();
                              qDebug() << "doc value : " << arr[1].toString();
                          }
              
              

              i am getting resuls though

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