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. extract values from json result
Forum Update on Monday, May 27th 2025

extract values from json result

Scheduled Pinned Locked Moved Unsolved General and Desktop
json parserjson
5 Posts 3 Posters 975 Views
  • 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.
  • QjayQ Offline
    QjayQ Offline
    Qjay
    wrote on last edited by
    #1

    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
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      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
      2
      • QjayQ Offline
        QjayQ Offline
        Qjay
        wrote on last edited by
        #3

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

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

        jsulmJ 1 Reply Last reply
        0
        • QjayQ Qjay

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

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

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

          @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
          2
          • QjayQ Offline
            QjayQ Offline
            Qjay
            wrote on last edited by Qjay
            #5

            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
            0

            • Login

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