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 2.9k 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.
  • Negar_mgN Offline
    Negar_mgN Offline
    Negar_mg
    wrote on last edited by
    #5

    thank you,you are the best.

    1 Reply Last reply
    0
    • Negar_mgN Negar_mg

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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.

      Negar_mgN 1 Reply Last reply
      1
      • JonBJ JonB

        @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_mgN Offline
        Negar_mgN Offline
        Negar_mg
        wrote on last edited by Negar_mg
        #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?

        JonBJ 1 Reply Last reply
        0
        • Negar_mgN Negar_mg

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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.

          Negar_mgN 1 Reply Last reply
          2
          • JonBJ JonB

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

            Negar_mgN Offline
            Negar_mgN Offline
            Negar_mg
            wrote on last edited by Negar_mg
            #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?

            JonBJ 1 Reply Last reply
            0
            • Negar_mgN Negar_mg

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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.

              Negar_mgN 1 Reply Last reply
              1
              • JonBJ JonB

                @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_mgN Offline
                Negar_mgN Offline
                Negar_mg
                wrote on last edited by
                #11

                @JonB
                thanks alot

                1 Reply Last reply
                0
                • Negar_mgN Offline
                  Negar_mgN Offline
                  Negar_mg
                  wrote on 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 Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 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

                    Negar_mgN 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      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.

                      Negar_mgN Offline
                      Negar_mgN Offline
                      Negar_mg
                      wrote on 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
                      0
                      • Negar_mgN Negar_mg

                        @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 last edited by mrjj
                        #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

                        Negar_mgN 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @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_mgN Offline
                          Negar_mgN Offline
                          Negar_mg
                          wrote on last edited by
                          #16

                          @mrjj
                          thank you

                          1 Reply Last reply
                          0
                          • _ Offline
                            _ Offline
                            _-mohamed-_
                            wrote on 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

                            Negar_mgN 1 Reply Last reply
                            0
                            • _ _-mohamed-_

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

                              Negar_mgN Offline
                              Negar_mgN Offline
                              Negar_mg
                              wrote on 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
                              0
                              • Negar_mgN Negar_mg

                                @_-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 last edited by
                                #19

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

                                Negar_mgN 1 Reply Last reply
                                0
                                • _ _-mohamed-_

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

                                  Negar_mgN Offline
                                  Negar_mgN Offline
                                  Negar_mg
                                  wrote on 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
                                  0
                                  • Negar_mgN Negar_mg

                                    @_-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 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
                                    • Negar_mgN Negar_mg

                                      @_-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 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
                                      • Negar_mgN Offline
                                        Negar_mgN Offline
                                        Negar_mg
                                        wrote on last edited by
                                        #23

                                        Thank you all

                                        1 Reply Last reply
                                        0
                                        • Negar_mgN Offline
                                          Negar_mgN Offline
                                          Negar_mg
                                          wrote on 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