Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use Json data in Qt in Windows
Forum Updated to NodeBB v4.3 + New Features

Use Json data in Qt in Windows

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 7 Posters 3.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Negar_mg
    wrote on 1 Sept 2021, 05:26 last edited by
    #5

    thank you,you are the best.

    1 Reply Last reply
    0
    • N Negar_mg
      1 Sept 2021, 05:04

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

      J Offline
      J Offline
      JonB
      wrote on 1 Sept 2021, 08:31 last edited by
      #6

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

      N 1 Reply Last reply 11 Sept 2021, 11:24
      1
      • J JonB
        1 Sept 2021, 08:31

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

        N Offline
        N Offline
        Negar_mg
        wrote on 11 Sept 2021, 11:24 last edited by Negar_mg 9 Nov 2021, 11:25
        #7

        @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?

        J 1 Reply Last reply 11 Sept 2021, 12:27
        0
        • N Negar_mg
          11 Sept 2021, 11:24

          @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?

          J Offline
          J Offline
          JonB
          wrote on 11 Sept 2021, 12:27 last edited by
          #8

          @Negar_mg
          As you can read at https://doc.qt.io/qt-5/qjsondocument.html#details

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

          N 1 Reply Last reply 12 Sept 2021, 05:38
          2
          • J JonB
            11 Sept 2021, 12:27

            @Negar_mg
            As you can read at https://doc.qt.io/qt-5/qjsondocument.html#details

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

            N Offline
            N Offline
            Negar_mg
            wrote on 12 Sept 2021, 05:38 last edited by Negar_mg 9 Dec 2021, 05:44
            #9

            @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?

            J 1 Reply Last reply 12 Sept 2021, 06:45
            0
            • N Negar_mg
              12 Sept 2021, 05:38

              @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?

              J Offline
              J Offline
              JonB
              wrote on 12 Sept 2021, 06:45 last edited by
              #10

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

              N 1 Reply Last reply 12 Sept 2021, 06:55
              1
              • J JonB
                12 Sept 2021, 06:45

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

                N Offline
                N Offline
                Negar_mg
                wrote on 12 Sept 2021, 06:55 last edited by
                #11

                @JonB
                thanks alot

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  Negar_mg
                  wrote on 12 Sept 2021, 09:49 last edited by
                  #12

                  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;
                  
                  
                       }
                  
                  
                     }
                  

                  2.png

                  The data should be as follows, but as you can see in the first image, the data is not readable
                  1.png

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 12 Sept 2021, 09:52 last edited by
                    #13

                    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.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    N 1 Reply Last reply 12 Sept 2021, 10:03
                    0
                    • Christian EhrlicherC Christian Ehrlicher
                      12 Sept 2021, 09:52

                      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.

                      N Offline
                      N Offline
                      Negar_mg
                      wrote on 12 Sept 2021, 10:03 last edited by
                      #14

                      @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?

                      mrjjM 1 Reply Last reply 12 Sept 2021, 10:33
                      0
                      • N Negar_mg
                        12 Sept 2021, 10:03

                        @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?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 12 Sept 2021, 10:33 last edited by mrjj 9 Dec 2021, 10:36
                        #15

                        @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

                        N 1 Reply Last reply 12 Sept 2021, 11:12
                        1
                        • mrjjM mrjj
                          12 Sept 2021, 10:33

                          @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

                          N Offline
                          N Offline
                          Negar_mg
                          wrote on 12 Sept 2021, 11:12 last edited by
                          #16

                          @mrjj
                          thank you

                          1 Reply Last reply
                          0
                          • _ Offline
                            _ Offline
                            _-mohamed-_
                            wrote on 12 Sept 2021, 11:38 last edited by
                            #17

                            @Negar_mg this is the best example, it helped me a lot:
                            Example of how to use JSON file with QT

                            N 1 Reply Last reply 12 Sept 2021, 11:58
                            0
                            • _ _-mohamed-_
                              12 Sept 2021, 11:38

                              @Negar_mg this is the best example, it helped me a lot:
                              Example of how to use JSON file with QT

                              N Offline
                              N Offline
                              Negar_mg
                              wrote on 12 Sept 2021, 11:58 last edited by
                              #18

                              @_-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

                              _ jsulmJ 2 Replies Last reply 12 Sept 2021, 12:07
                              0
                              • N Negar_mg
                                12 Sept 2021, 11:58

                                @_-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

                                _ Offline
                                _ Offline
                                _-mohamed-_
                                wrote on 12 Sept 2021, 12:07 last edited by
                                #19

                                @Negar_mg do you mean that you want to save the final result in a json file?

                                N 1 Reply Last reply 12 Sept 2021, 12:10
                                0
                                • _ _-mohamed-_
                                  12 Sept 2021, 12:07

                                  @Negar_mg do you mean that you want to save the final result in a json file?

                                  N Offline
                                  N Offline
                                  Negar_mg
                                  wrote on 12 Sept 2021, 12:10 last edited by
                                  #20

                                  @_-mohamed-_
                                  i want to save this {" dht11Data \ ": [29,27]}
                                  And I want to display each of the numbers like 27 in one lineEdit.

                                  _ 1 Reply Last reply 12 Sept 2021, 14:10
                                  0
                                  • N Negar_mg
                                    12 Sept 2021, 12:10

                                    @_-mohamed-_
                                    i want to save this {" dht11Data \ ": [29,27]}
                                    And I want to display each of the numbers like 27 in one lineEdit.

                                    _ Offline
                                    _ Offline
                                    _-mohamed-_
                                    wrote on 12 Sept 2021, 14:10 last edited by
                                    #21

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

                                    1 Reply Last reply
                                    1
                                    • N Negar_mg
                                      12 Sept 2021, 11:58

                                      @_-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

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 13 Sept 2021, 14:42 last edited by
                                      #22

                                      @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!

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      4
                                      • N Offline
                                        N Offline
                                        Negar_mg
                                        wrote on 14 Sept 2021, 04:02 last edited by
                                        #23

                                        Thank you all

                                        1 Reply Last reply
                                        0
                                        • N Offline
                                          N Offline
                                          Negar_mg
                                          wrote on 14 Sept 2021, 11:11 last edited by Negar_mg
                                          #24

                                          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;
                                          }
                                          
                                          1 Reply Last reply
                                          2

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved