Home > Back-end >  How I will get particular value of key in this below output line in Qt?
How I will get particular value of key in this below output line in Qt?

Time:12-07

here is my code.

QJsonObject distance = QJsonDocument::fromJson(databuffer).object();
qDebug()<<distance["feeds"];

now my output is:

QJsonValue(array, QJsonArray([{"created_at":"2021-12-06T06:11:23Z","entry_id":21,"field1":"202"}]))

i need only the value of particular "field1" key's value. how can i get it ? please help.

CodePudding user response:

Use distance.value("feeds").toArray().at(0).toObject().value("field1").

Of course you should add some error handling in case the data is not in the form as expected. But you probably want to make it more general based on your use case...

And a little explanation: QJsonValue is a general value holder and before actual use it should be converted to actual value (int, double, QString, bool etc.) or a to container QJsonArray, which can be read with position index, or object QJsonObject, which can be read by field names.

  • Related