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.
  • N Offline
    N Offline
    Negar_mg
    wrote on 1 Sept 2021, 05:04 last edited by Negar_mg 9 Jan 2021, 05:15
    #1

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

    E J 2 Replies Last reply 1 Sept 2021, 05:08
    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 ...

      E Offline
      E Offline
      eyllanesc
      wrote on 1 Sept 2021, 05:08 last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • N Offline
        N Offline
        Negar_mg
        wrote on 1 Sept 2021, 05:15 last edited by
        #3

        sorry,I wanted to use Json

        E 1 Reply Last reply 1 Sept 2021, 05:17
        0
        • N Negar_mg
          1 Sept 2021, 05:15

          sorry,I wanted to use Json

          E Offline
          E Offline
          eyllanesc
          wrote on 1 Sept 2021, 05:17 last edited by
          #4

          @Negar_mg See https://doc.qt.io/qt-5/qjsondocument.html

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

                                          • Login

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