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

5
  • So you're using C++? Or what? There are certainly libraries designed to parse JSON, e.g. see this comparison. Commented Feb 28, 2018 at 7:06
  • yes, i'm using c++ Commented Feb 28, 2018 at 7: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. Commented Feb 28, 2018 at 7: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 Commented Feb 28, 2018 at 7:14
  • Oh okay, I see. Please see if this answer also works for you, and if not, edit your question to make it more clear what exactly you are asking. Commented Feb 28, 2018 at 7:15

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.