constrcuteur parse json

This commit is contained in:
p2405951 2025-06-19 09:51:06 +02:00
parent 5f2bb19da9
commit b2f95da02c
7 changed files with 120 additions and 14 deletions

View File

@ -0,0 +1,21 @@
QMAKE_CXX.QT_COMPILER_STDCXX = 201703L
QMAKE_CXX.QMAKE_GCC_MAJOR_VERSION = 13
QMAKE_CXX.QMAKE_GCC_MINOR_VERSION = 1
QMAKE_CXX.QMAKE_GCC_PATCH_VERSION = 0
QMAKE_CXX.COMPILER_MACROS = \
QT_COMPILER_STDCXX \
QMAKE_GCC_MAJOR_VERSION \
QMAKE_GCC_MINOR_VERSION \
QMAKE_GCC_PATCH_VERSION
QMAKE_CXX.INCDIRS = \
C:/Qt/Tools/mingw1310_64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++ \
C:/Qt/Tools/mingw1310_64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/x86_64-w64-mingw32 \
C:/Qt/Tools/mingw1310_64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/backward \
C:/Qt/Tools/mingw1310_64/lib/gcc/x86_64-w64-mingw32/13.1.0/include \
C:/Qt/Tools/mingw1310_64/lib/gcc/x86_64-w64-mingw32/13.1.0/include-fixed \
C:/Qt/Tools/mingw1310_64/x86_64-w64-mingw32/include
QMAKE_CXX.LIBDIRS = \
C:/Qt/Tools/mingw1310_64/lib/gcc/x86_64-w64-mingw32/13.1.0 \
C:/Qt/Tools/mingw1310_64/lib/gcc \
C:/Qt/Tools/mingw1310_64/x86_64-w64-mingw32/lib \
C:/Qt/Tools/mingw1310_64/lib

5
data.qrc Normal file
View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/data">
<file>ex.txt</file>
</qresource>
</RCC>

7
ex.txt Normal file
View File

@ -0,0 +1,7 @@
{
"id": 1,
"texte": "tkt",
"latitude": "N 45 37.199",
"longitude": "W 1 2.009",
"valeur": -1
}

View File

@ -2,13 +2,22 @@
#include <QApplication> #include <QApplication>
#include <step.h> #include <step.h>
#include <QFile>
using namespace std;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
MainWindow w; MainWindow w;
w.show(); //w.show();
QFile file(":/data/ex.txt");
file.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream in(&file);
Step v();
/* QString contenu = in.readAll();
qDebug() << "Contenu lu depuis QTextStream:";
qDebug().noquote() << contenu;*/
return a.exec(); return a.exec();
} }

View File

@ -24,3 +24,7 @@ FORMS += \
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
RESOURCES += \
data.qrc

View File

@ -1,26 +1,77 @@
#include "step.h" #include "step.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonParseError>
QString Step::getTitle() const
{
return title;
}
float Step::getLatitude() const
{
return latitude;
}
float Step::getLongitude() const
{
return longitude;
}
int Step::getResponse() const
{
return response;
}
Step::Step() { Step::Step() {
latitude = 0.0; latitude = 0.0;
longitude = 0.0; longitude = 0.0;
response = 0; response = 0;
} }
Step::Step(QTextStream &in)
Step::Step( QJsonObject &in)
{ {
int stepNumber; title = in["title"].toString();
in >> stepNumber; response = in["reponse"].toInt();
in.readLine(); QString gps = in["GPS"].toString();
title = in.readLine(); QStringList parts = gps.split(" ", Qt::SkipEmptyParts);
QChar latDir, lonDir;
int latDeg, lonDeg; QChar latDir = parts[0][0]; // c'est le premier QChar du QString t'as capté
float latMin, lonMin; int latDeg = parts[1].toInt();
in >> latDir >> latDeg >> latMin >> lonDir >> lonDeg >> lonMin; float latMin = parts[2].toFloat();
setLatitude(latDeg, latMin, latDir); setLatitude(latDeg, latMin, latDir);
QChar lonDir = parts[3][0];
int lonDeg = parts[4].toInt();
float lonMin = parts[5].toFloat();
setLongitude(lonDeg, lonMin, lonDir); setLongitude(lonDeg, lonMin, lonDir);
in >> response;
in.readLine(); QJsonArray personnagetab = in["dialogue"].toArray();
for (const QJsonValue &val : personnagetab) {
if (val.isObject()) {
QJsonObject obj = val.toObject();
QString personnage = obj["personnage"].toString();
personnage.append(personnage);
} }
}
QJsonArray textetab = in["dialogue"].toArray();
for (const QJsonValue &val : textetab) {
if (val.isObject()) {
QJsonObject obj = val.toObject();
QString textes = obj["texte"].toString();
texte.append(textes);
}
}
}
void Step::setLatitude(int degree, float minute, QChar NS) void Step::setLatitude(int degree, float minute, QChar NS)
{ {
latitude = degree + minute / 60.0; latitude = degree + minute / 60.0;

13
step.h
View File

@ -2,6 +2,10 @@
#define STEP_H #define STEP_H
#include <QString> #include <QString>
#include <QTextStream> #include <QTextStream>
#include <iostream>
#include <QJsonObject>
#include <QList>
using namespace std;
class Step class Step
{ {
@ -9,14 +13,19 @@ private:
QString title; QString title;
float latitude; float latitude;
float longitude; float longitude;
QString text;
int response; int response;
QList<QString> personnage;
QList<QString> texte;
public: public:
Step(); Step();
Step(QTextStream &in); Step(QJsonObject &in);
void setLatitude(int degree,float minute,QChar NS); void setLatitude(int degree,float minute,QChar NS);
void setLongitude(int degree,float minute,QChar EW); void setLongitude(int degree,float minute,QChar EW);
QString getTitle() const;
float getLatitude() const;
float getLongitude() const;
int getResponse() const;
}; };
#endif // STEP_H #endif // STEP_H