Use Json data in Qt in Windows
-
I used the following code and was able to get Json data from the serial port.
But the data is displayed as I sent it to you in the image, how can I separate it and store each in a different variable?serial.setPortName("COM3"); serial.open(QIODevice::ReadWrite); serial.setBaudRate(QSerialPort::Baud9600); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); while(!serial.isOpen()) serial.open(QIODevice::ReadWrite); if (serial.isOpen() && serial.isWritable()) { qDebug() << "Serial is open"; QByteArray output; QByteArray input; while(true) { output = "a"; serial.write(output); serial.flush(); serial.waitForBytesWritten(1000); serial.waitForReadyRead(1000); input = serial.readAll(); qDebug()<<input; } }
The data should be as follows, but as you can see in the first image, the data is not readable
-
I don't see a difference between those two pictures. The json content is exactly the same. If you want to parse the json you should take a look at the documentation of QJsonDocument and it's corresponding classes.
-
I don't see a difference between those two pictures. The json content is exactly the same. If you want to parse the json you should take a look at the documentation of QJsonDocument and it's corresponding classes.
@Christian-Ehrlicher
Thanks, how can I separate the data? I do not know how to use QJsonDocument?
Can you guide me with an example? For example, what should I do to store the amount of Bat_Temp data in the x variable? -
@Christian-Ehrlicher
Thanks, how can I separate the data? I do not know how to use QJsonDocument?
Can you guide me with an example? For example, what should I do to store the amount of Bat_Temp data in the x variable?@Negar_mg said in Use Json data in Qt in Windows:
QJsonDocument
QString input = "{Bat_temp:19}"; // you used a picture so could not get real input // the toUTF is due to it wants a bytearrya not qstring. QJsonDocument jsonResponse = QJsonDocument::fromJson(input.toUtf8()); // ask for a json object QJsonObject jsonObject = jsonResponse.object(); //access a property of that json object int value = jsonObject["Bat_temp"].toInt();
for dht11Data you must use QJsonArray
-
@Negar_mg said in Use Json data in Qt in Windows:
QJsonDocument
QString input = "{Bat_temp:19}"; // you used a picture so could not get real input // the toUTF is due to it wants a bytearrya not qstring. QJsonDocument jsonResponse = QJsonDocument::fromJson(input.toUtf8()); // ask for a json object QJsonObject jsonObject = jsonResponse.object(); //access a property of that json object int value = jsonObject["Bat_temp"].toInt();
for dht11Data you must use QJsonArray
-
@Negar_mg this is the best example, it helped me a lot:
Example of how to use JSON file with QT -
@Negar_mg this is the best example, it helped me a lot:
Example of how to use JSON file with QT@_-mohamed-_
thanks a lot
You know, my problem is that I receive data from the Arduino from the serial port
As I wrote in the above code, I can print data with an infinite loop.
As follows :while (true) { serial.waitForReadyRead (100000); input = input.append (serial.readAll ()); qDebug () << input; }
And the output is as follows:
//..............................................................................................................................
Serial is open
"{"
"{" dht "
"{" dht11Da "
"{" dht11Data \ ":"
"{" dht11Data \ ": [29,"
"{" dht11Data \ ": [29,27]}"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht1 "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Da "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ":"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r \ n]"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r \ n] \ r \ n } "
//..........................................................................................................................................
No data is received at first, so I do not know how to manage this infinite loop and store the data in an array -
@_-mohamed-_
thanks a lot
You know, my problem is that I receive data from the Arduino from the serial port
As I wrote in the above code, I can print data with an infinite loop.
As follows :while (true) { serial.waitForReadyRead (100000); input = input.append (serial.readAll ()); qDebug () << input; }
And the output is as follows:
//..............................................................................................................................
Serial is open
"{"
"{" dht "
"{" dht11Da "
"{" dht11Data \ ":"
"{" dht11Data \ ": [29,"
"{" dht11Data \ ": [29,27]}"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht1 "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Da "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ":"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r \ n]"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r \ n] \ r \ n } "
//..........................................................................................................................................
No data is received at first, so I do not know how to manage this infinite loop and store the data in an array@Negar_mg do you mean that you want to save the final result in a json file?
-
@Negar_mg do you mean that you want to save the final result in a json file?
@_-mohamed-_
i want to save this {" dht11Data \ ": [29,27]}
And I want to display each of the numbers like 27 in one lineEdit. -
@_-mohamed-_
i want to save this {" dht11Data \ ": [29,27]}
And I want to display each of the numbers like 27 in one lineEdit.@Negar_mg You can't use a backslash in the key, you can use FrontSlash instead
This is the code you need:
#include <QApplication> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QByteArray> #include <QJsonValue> #include <QDebug> #include <QFile> int main(int argc, char *argv[]) { QApplication a(argc, argv); QFile file("C:/Users/moham/OneDrive/Desktop/json file.json"); //write the file path here if(!file.open(QFile::ReadOnly)) //opening file qDebug() << "can't open the file"; else { QByteArray fileContents = file.readAll(); // reading the file file.close(); //closing file QJsonDocument jsonDoc = QJsonDocument::fromJson(fileContents); QJsonObject jsonObj = jsonDoc.object(); QJsonArray jsonArray = jsonObj[" dht11Data / "].toArray(); foreach(QJsonValue v, jsonArray) //printing array's values qDebug() << v.toInt(); // return 0; } return a.exec(); }
this code reads (JSON file.json) and prints it in the console, you can use the values wherever you want.
I hope I helped you.
-
@_-mohamed-_
thanks a lot
You know, my problem is that I receive data from the Arduino from the serial port
As I wrote in the above code, I can print data with an infinite loop.
As follows :while (true) { serial.waitForReadyRead (100000); input = input.append (serial.readAll ()); qDebug () << input; }
And the output is as follows:
//..............................................................................................................................
Serial is open
"{"
"{" dht "
"{" dht11Da "
"{" dht11Data \ ":"
"{" dht11Data \ ": [29,"
"{" dht11Data \ ": [29,27]}"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht1 "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Da "
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ":"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r \ n]"
"{" dht11Data \ ": [29,27]} \ r \ n {\ r \ n " dht11Data \ ": [\ r \ n 29, \ r \ n 27 \ r \ n] \ r \ n } "
//..........................................................................................................................................
No data is received at first, so I do not know how to manage this infinite loop and store the data in an array@Negar_mg said in Use Json data in Qt in Windows:
infinite loop
Stop using infinite loops first!
Please take a look at documentation and examples to see how to properly use QSerialPort: https://doc.qt.io/qt-5/qtserialport-creaderasync-example.html
To read from serial port connect a slot to https://doc.qt.io/qt-5/qiodevice.html#readyRead signal and use readAll() in this slot. Keep in mind that you have to accumulate the data read from serial port until you got whole JSON document! -
I finally succeeded,The code I wrote is as follows:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); m_serialPort=new QSerialPort; m_serialPort->setPortName("COM3"); m_serialPort->open(QIODevice::ReadWrite); m_serialPort->setBaudRate(QSerialPort::Baud9600); m_serialPort->setDataBits(QSerialPort::Data8); m_serialPort->setParity(QSerialPort::NoParity); m_serialPort->setStopBits(QSerialPort::OneStop); m_serialPort->setFlowControl(QSerialPort::NoFlowControl); while(!m_serialPort->isOpen()) m_serialPort->open(QIODevice::ReadWrite); connect(m_serialPort, &QSerialPort::readyRead, this, &MainWindow::handleReadyRead); connect(&m_timer, &QTimer::timeout, this, &MainWindow::handleTimeout); m_timer.start(5000); } void MainWindow::handleReadyRead() { m_readData.append(m_serialPort->readAll()); if (!m_timer.isActive()) m_timer.start(5000); } void MainWindow::handleTimeout() { if (m_readData.isEmpty()) { qDebug()<< "m_readData is empty" <<m_readData; } else { qDebug()<< "m_readData is full" <<m_readData; QJsonDocument jsonDoc = QJsonDocument::fromJson(m_readData); QJsonObject jsonObj = jsonDoc.object(); QJsonValue jsonVal; jsonVal = jsonObj.value(QString("Bat_Temp")); QTextStream textStream(stdout); textStream << QString::number(jsonVal.toInt()) << endl; }