1

i am new to QT and i have created a HTTP server, the server will run on windows and client on android, server will get POST request and response in JSON, something like that:

Json response:

[
     {         
    "FName": "fname",
    "LName": "lname",
    "MobileNo": 0000000000000,
    "Passwd": "a56df67a6sd8fa9ds8f0ads89f8asd7f",
    "UserName": "testexample",
    "authority": 3,
    "id": 10,
    "images": "/files/team1.jpg"
},
{
    "FName": "super",
    "LName": "visor",
    "MobileNo": 1111111111111,
    "Passwd": "78ad78af97sd89f7a9sd7f9as7df8asd7f",
    "UserName": "supervisor",
    "authority": 2,
    "id": 11,
    "images": "/files/team1.jpg"
}]

server code:

   QJsonDocument  json;
  QJsonArray     recordsArray;

  while(query.next())
  {
     QJsonObject recordObject;
        for(int x=0; x < query.record().count(); x++)
        {
            recordObject.insert( query.record().fieldName(x),QJsonValue::fromVariant(query.value(x)) );
        }
     recordsArray.push_back(recordObject);
  }
  json.setArray(recordsArray);

  return json.toJson();

how can i get FName, LName from all dynamic objects. i want to use qt class because i am creating client side for android in Qt.

i am using QT5.9.2 on windows any example........

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • So you're using C++? Or what? There are certainly libraries designed to parse JSON, e.g. see [this comparison](https://github.com/miloyip/nativejson-benchmark#libraries). – Thomas Flinkow Feb 28 '18 at 07:06
  • yes, i'm using c++ – Sufyan Mughal Feb 28 '18 at 07:09
  • So, where exactly is the problem? Find a JSON library you would like to use, and see through the examples they provide. If I understand correctly, and this question is about parsing the JSON, this should help. – Thomas Flinkow Feb 28 '18 at 07:10
  • i am making android app in qt creator as we know qt is cross platform so i want to use qt class like QJsonArray, QJsonObject. how can i get these value because after i will also create for ios.....any example please.....Thanks for reply – Sufyan Mughal Feb 28 '18 at 07:14
  • Oh okay, I see. Please see if [this](https://stackoverflow.com/a/19823441/7571171) answer also works for you, and if not, edit your question to make it more clear what exactly you are asking. – Thomas Flinkow Feb 28 '18 at 07:15

1 Answers1

0

Solution:

QJsonDocument doc = QJsonDocument::fromJson(json.toJson());
QJsonArray ary = doc.array();
foreach (const QJsonValue & value, ary) 
{
    QJsonObject obj = value.toObject();
    qDebug() <<"test: "<< obj["FName"].toString();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241