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

extract values from json result



  • 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;
    

  • Lifetime Qt Champion

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



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

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


  • Lifetime Qt Champion

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



  • 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


Log in to reply