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 Updated to NodeBB v4.3 + New Features

extract values from json result

Scheduled Pinned Locked Moved Unsolved General and Desktop
json parserjson
5 Posts 3 Posters 1.0k Views 2 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.
  • Q Offline
    Q Offline
    Qjay
    wrote on 15 Jul 2019, 12:01 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 15 Jul 2019, 12:31 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
      • Q Offline
        Q Offline
        Qjay
        wrote on 15 Jul 2019, 12:56 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.

        J 1 Reply Last reply 15 Jul 2019, 13:21
        0
        • Q Qjay
          15 Jul 2019, 12:56

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

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 15 Jul 2019, 13:21 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
          • Q Offline
            Q Offline
            Qjay
            wrote on 15 Jul 2019, 13:43 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

            5/5

            15 Jul 2019, 13:43

            • Login

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