Cm Jagtap
Cm Jagtap

Reputation: 177

How to read specific record from JSON file in QT

I have this code

  QString val;
  QFile file;
  file.setFileName("MissionWaypoints.json");
  file.open(QIODevice::ReadOnly | QIODevice::Text);
  val = file.readAll();
  file.close();
  QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
  qDebug() << doc;
  QJsonObject sett2 = doc.object();
  qDebug() << sett2;

And my JSON file is like this

    [
        {
            "waypoints": {
                "AutoContinue": 1,
                "Command": 16,
                "Current": 0,
                "Frame": 0,
                "MSG x": 0,
                "Mission type ": 0,
                "Msg y": 0,
                "Msg z": 0,
                "Param 1": 0,
                "Param 2": 0,
                "Param 3": 0,
                "Param 4": 0,
                "Sequence no": 0,
                "Target Component": 0
            }
        },
        {
            "waypoints": {
                "AutoContinue": 1,
                "Command": 22,
                "Current": 0,
                "Frame": 3,
                "MSG x": 0,
                "Mission type ": 0,
                "Msg y": 0,
                "Msg z": 10,
                "Param 1": 15,
                "Param 2": 0,
                "Param 3": 0,
                "Param 4": 0,
                "Sequence no": 1,
                "Target Component": 0
            }
        }
]

I have crated sett2 JSON object, then i tried to print sett2 object but it shows null , also when i printed doc it shows json file contents. anyone know how to convert this doc into JSON object ??

Upvotes: 0

Views: 172

Answers (2)

Cm Jagtap
Cm Jagtap

Reputation: 177

 QByteArray val;  // don't use QString, the 2-time conversion is pointless
 QFile file;
 file.setFileName("MissionWaypoints.json");
 file.open(QIODevice::ReadOnly | QIODevice::Text);
 val = file.readAll();
 file.close();

 QJsonParseError jspe{};
 const QJsonDocument doc = QJsonDocument::fromJson(val, &jspe);
 if (doc.isNull()) {
   qWarning() << "Error loading JSON:" << jspe.errorString() << "@" << jspe.offset;
   return;
 }
// qDebug() << doc;
 if (doc.isArray())
   qDebug() << "Document is an array" << doc.array();
 else if (doc.isObject())
   qDebug() << "Document is an object" << doc.object();

 QJsonArray jsonArray = doc.array();


 for (int i=0; i < jsonArray.size(); i++)
 {
     QJsonObject temp =  jsonArray.at(i).toObject();
     qDebug() << temp.value("waypoints").toObject().value("Sequence no");
     qDebug() <<" Latitude " <<temp.value("waypoints").toObject().value("MSG x");
     qDebug() <<" Longitude " <<temp.value("waypoints").toObject().value("Msg y");

 }

Thank you @Maxim i have taken your code and converted doc into jsonArray and iterated it. so found my solution Thanks

Upvotes: 0

Maxim Paperno
Maxim Paperno

Reputation: 4869

The base type in that JSON document is an array, not an object.


  QByteArray val;  // don't use QString, the 2-time conversion is pointless
  QFile file;
  file.setFileName("MissionWaypoints.json");
  file.open(QIODevice::ReadOnly | QIODevice::Text);
  val = file.readAll();
  file.close();

  QJsonParseError jspe{};
  const QJsonDocument doc = QJsonDocument::fromJson(val, &jspe);
  if (doc.isNull()) {
    qWarning() << "Error loading JSON:" << jspe.errorString() << "@" << jspe.offset;
    return;
  }
  qDebug() << doc;
  if (doc.isArray())
    qDebug() << "Document is an array" << doc.array();
  else if (doc.isObject())
    qDebug() << "Document is an object" << doc.object();

Upvotes: 1

Related Questions