Use Json data in Qt in Windows
-
wrote on 1 Sept 2021, 05:04 last edited by Negar_mg 9 Jan 2021, 05:15
Hi,
I wanted to use Json data in qt using CPP language. Read Json data and send Json data.
How do I add Json to qt_creator?
And how do I receive and send Json data?
I use Windows10 and I use MSVC2015 64bit in Qt5.
Any suggestion can be useful to me, thank you ... -
Hi,
I wanted to use Json data in qt using CPP language. Read Json data and send Json data.
How do I add Json to qt_creator?
And how do I receive and send Json data?
I use Windows10 and I use MSVC2015 64bit in Qt5.
Any suggestion can be useful to me, thank you ...wrote on 1 Sept 2021, 05:08 last edited byThis post is deleted! -
wrote on 1 Sept 2021, 05:15 last edited by
sorry,I wanted to use Json
-
wrote on 1 Sept 2021, 05:17 last edited by
-
wrote on 1 Sept 2021, 05:26 last edited by
thank you,you are the best.
-
Hi,
I wanted to use Json data in qt using CPP language. Read Json data and send Json data.
How do I add Json to qt_creator?
And how do I receive and send Json data?
I use Windows10 and I use MSVC2015 64bit in Qt5.
Any suggestion can be useful to me, thank you ...wrote on 1 Sept 2021, 08:31 last edited by@Negar_mg said in Use Json data in Qt in Windows:
How do I add Json to qt_creator?
That doesn't exist, Creator itself neither cares nor knows about JSON. Designer does not use it. You may use JSON in code you happen to write in Creator's code editor, but it's nothing to do with Creator. Just so you know.
-
@Negar_mg said in Use Json data in Qt in Windows:
How do I add Json to qt_creator?
That doesn't exist, Creator itself neither cares nor knows about JSON. Designer does not use it. You may use JSON in code you happen to write in Creator's code editor, but it's nothing to do with Creator. Just so you know.
-
@JonB
Thanks , can I use QJsonDocument Class to read json from serial port?
Is it possible to read and write this data in qt or c ++ libraries?wrote on 11 Sept 2021, 12:27 last edited by@Negar_mg
As you can read at https://doc.qt.io/qt-5/qjsondocument.html#detailsQJsonDocument is a class that wraps a complete JSON document and can read and write this document both from a UTF-8 encoded text based representation as well as Qt's own binary format.
Since you will use QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr) you will need a
QByteArray
. It does not do the reading, you will need to fetch the bytes from the serial port.Is it possible to read and write this data in qt or c ++ libraries?
Read and write what data? Serial port? Yes. JSON? Yes.
-
@Negar_mg
As you can read at https://doc.qt.io/qt-5/qjsondocument.html#detailsQJsonDocument is a class that wraps a complete JSON document and can read and write this document both from a UTF-8 encoded text based representation as well as Qt's own binary format.
Since you will use QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr) you will need a
QByteArray
. It does not do the reading, you will need to fetch the bytes from the serial port.Is it possible to read and write this data in qt or c ++ libraries?
Read and write what data? Serial port? Yes. JSON? Yes.
-
@JonB
Thanks for the explanation. I'm trying it now
I just had one more question, should I use QSerialPort Class to fetch Json data from the serial port?wrote on 12 Sept 2021, 06:45 last edited by@Negar_mg
Yes that's fine. Try to separate serial data and JSON data in your head. Serial data is just bytes, it could be anything. JSON is treat bytes as text and parse it as JSON. Two unconnected things, you can put them together if it suits you. -
@Negar_mg
Yes that's fine. Try to separate serial data and JSON data in your head. Serial data is just bytes, it could be anything. JSON is treat bytes as text and parse it as JSON. Two unconnected things, you can put them together if it suits you. -
wrote on 12 Sept 2021, 09:49 last edited by
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.
wrote on 12 Sept 2021, 10:03 last edited by@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?Lifetime Qt Championwrote on 12 Sept 2021, 10:33 last edited by mrjj 9 Dec 2021, 10:36@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
-
wrote on 12 Sept 2021, 11:38 last edited by
@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 QTwrote on 12 Sept 2021, 11:58 last edited by@_-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 arraywrote on 12 Sept 2021, 12:07 last edited by@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?
wrote on 12 Sept 2021, 12:10 last edited by@_-mohamed-_
i want to save this {" dht11Data \ ": [29,27]}
And I want to display each of the numbers like 27 in one lineEdit.