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. How to exploit a json file in QT ?

How to exploit a json file in QT ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
jsoncpp
28 Posts 4 Posters 4.7k Views
  • 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.
  • F Offline
    F Offline
    feedain
    wrote on 19 Jan 2022, 09:26 last edited by feedain
    #1

    Good morning everyone,
    I have a project that is to communicate in mqtt with some arduino's(that's ok) but i need to exploit the data (such as the port of arduino or the state : 0 or 1) in JSON format, so i created a json file in my mainwindow.cpp :

           //JSON
    
           QJsonObject PortObject;
           PortObject.insert("Port", QJsonValue::fromVariant("p"));
    
           QJsonObject StateObject;
           StateObject.insert("Etat", "e");
    
    
           QJsonDocument doc(PortObject);
           qDebug() << doc.toJson();
    

    The first QJsonObject is for which port i want to change his state et the second is to change the state.
    I want to know ```
    if fromVariant("p"));

    StateObject.insert("Etat", "e");
    

    is good as :

    with p for the port number and e the state : 0 or 1
    
    And likely to exploit this json file on output application to change a state of a port (for instance).
    
    Thanks everyone.
    J F 2 Replies Last reply 19 Jan 2022, 09:41
    0
    • F feedain
      19 Jan 2022, 09:26

      Good morning everyone,
      I have a project that is to communicate in mqtt with some arduino's(that's ok) but i need to exploit the data (such as the port of arduino or the state : 0 or 1) in JSON format, so i created a json file in my mainwindow.cpp :

             //JSON
      
             QJsonObject PortObject;
             PortObject.insert("Port", QJsonValue::fromVariant("p"));
      
             QJsonObject StateObject;
             StateObject.insert("Etat", "e");
      
      
             QJsonDocument doc(PortObject);
             qDebug() << doc.toJson();
      

      The first QJsonObject is for which port i want to change his state et the second is to change the state.
      I want to know ```
      if fromVariant("p"));

      StateObject.insert("Etat", "e");
      

      is good as :

      with p for the port number and e the state : 0 or 1
      
      And likely to exploit this json file on output application to change a state of a port (for instance).
      
      Thanks everyone.
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 19 Jan 2022, 09:41 last edited by jsulm
      #2

      @feedain said in How to exploit a json file in QT ?:

      PortObject.insert("Port", QJsonValue::fromVariant("p"));

      What is this supposed to do?
      Port is a number, right? Not a string. Why not:

      PortObject.insert("Port", QJsonValue(p));
      

      ?

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

      J 1 Reply Last reply 19 Jan 2022, 09:45
      0
      • J jsulm
        19 Jan 2022, 09:41

        @feedain said in How to exploit a json file in QT ?:

        PortObject.insert("Port", QJsonValue::fromVariant("p"));

        What is this supposed to do?
        Port is a number, right? Not a string. Why not:

        PortObject.insert("Port", QJsonValue(p));
        

        ?

        J Offline
        J Offline
        JonB
        wrote on 19 Jan 2022, 09:45 last edited by JonB
        #3

        @jsulm said in How to exploit a json file in QT ?:

        QJsonValue(p)

        QJsonValue(p).toInt()?

        J 1 Reply Last reply 19 Jan 2022, 09:46
        0
        • J JonB
          19 Jan 2022, 09:45

          @jsulm said in How to exploit a json file in QT ?:

          QJsonValue(p)

          QJsonValue(p).toInt()?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 19 Jan 2022, 09:46 last edited by
          #4

          @JonB said in How to exploit a json file in QT ?:

          QJsonValue(p).toInt()?

          Why?
          insert() expects a QJsonValue.

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

          J 1 Reply Last reply 19 Jan 2022, 09:50
          1
          • J jsulm
            19 Jan 2022, 09:46

            @JonB said in How to exploit a json file in QT ?:

            QJsonValue(p).toInt()?

            Why?
            insert() expects a QJsonValue.

            J Offline
            J Offline
            JonB
            wrote on 19 Jan 2022, 09:50 last edited by JonB
            #5

            @jsulm
            I just read your

            Port is a number, right? Not a string

            If it's supposed to be a number (integer) for a port number i just like to get to converted when i get it from JSON. But not to worry, i think I mis-read what is going on here. I thought it was parsing JSON input, I thing you're talking about setting a JSON value, my bad.

            1 Reply Last reply
            0
            • F feedain
              19 Jan 2022, 09:26

              Good morning everyone,
              I have a project that is to communicate in mqtt with some arduino's(that's ok) but i need to exploit the data (such as the port of arduino or the state : 0 or 1) in JSON format, so i created a json file in my mainwindow.cpp :

                     //JSON
              
                     QJsonObject PortObject;
                     PortObject.insert("Port", QJsonValue::fromVariant("p"));
              
                     QJsonObject StateObject;
                     StateObject.insert("Etat", "e");
              
              
                     QJsonDocument doc(PortObject);
                     qDebug() << doc.toJson();
              

              The first QJsonObject is for which port i want to change his state et the second is to change the state.
              I want to know ```
              if fromVariant("p"));

              StateObject.insert("Etat", "e");
              

              is good as :

              with p for the port number and e the state : 0 or 1
              
              And likely to exploit this json file on output application to change a state of a port (for instance).
              
              Thanks everyone.
              F Offline
              F Offline
              feedain
              wrote on 19 Jan 2022, 10:06 last edited by
              #6

              @feedain said in How to exploit a json file in QT ?:

              Good morning everyone,
              I have a project that is to communicate in mqtt with some arduino's(that's ok) but i need to exploit the data (such as the port of arduino or the state : 0 or 1) in JSON format, so i created a json file in my mainwindow.cpp :

                     //JSON
              
                     QJsonObject PortObject;
                     PortObject.insert("Port", QJsonValue::fromVariant("p"));
              
                     QJsonObject StateObject;
                     StateObject.insert("Etat", "e");
              
              
                     QJsonDocument doc(PortObject);
                     qDebug() << doc.toJson();
              

              The first QJsonObject is for which port i want to change his state et the second is to change the state.
              I want to know

              > if fromVariant("p"));
              

              StateObject.insert("Etat", "e");

              is good as :
              
              with p for the port number and e the state : 0 or 1
              
              And likely to exploit this json file on output application to change a state of a port (for instance).
              
              Thanks everyone.
              J 1 Reply Last reply 19 Jan 2022, 10:08
              0
              • F feedain
                19 Jan 2022, 10:06

                @feedain said in How to exploit a json file in QT ?:

                Good morning everyone,
                I have a project that is to communicate in mqtt with some arduino's(that's ok) but i need to exploit the data (such as the port of arduino or the state : 0 or 1) in JSON format, so i created a json file in my mainwindow.cpp :

                       //JSON
                
                       QJsonObject PortObject;
                       PortObject.insert("Port", QJsonValue::fromVariant("p"));
                
                       QJsonObject StateObject;
                       StateObject.insert("Etat", "e");
                
                
                       QJsonDocument doc(PortObject);
                       qDebug() << doc.toJson();
                

                The first QJsonObject is for which port i want to change his state et the second is to change the state.
                I want to know

                > if fromVariant("p"));
                

                StateObject.insert("Etat", "e");

                is good as :
                
                with p for the port number and e the state : 0 or 1
                
                And likely to exploit this json file on output application to change a state of a port (for instance).
                
                Thanks everyone.
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 19 Jan 2022, 10:08 last edited by
                #7

                @feedain
                ?

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

                F 1 Reply Last reply 19 Jan 2022, 10:21
                1
                • J jsulm
                  19 Jan 2022, 10:08

                  @feedain
                  ?

                  F Offline
                  F Offline
                  feedain
                  wrote on 19 Jan 2022, 10:21 last edited by
                  #8

                  @jsulm said in How to exploit a json file in QT ?:

                  @feedain
                  ?

                  yes i modify my error but i want to know how can i exploit this data, for instance if i want to set up : the port 3 to state 1 ?

                  J 1 Reply Last reply 19 Jan 2022, 10:27
                  0
                  • F feedain
                    19 Jan 2022, 10:21

                    @jsulm said in How to exploit a json file in QT ?:

                    @feedain
                    ?

                    yes i modify my error but i want to know how can i exploit this data, for instance if i want to set up : the port 3 to state 1 ?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 19 Jan 2022, 10:27 last edited by
                    #9

                    @feedain Please explain better what you want to achieve.
                    Can you please post JSON document you want to create? So thatb we can actually understand what you are asking.

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

                    F 1 Reply Last reply 19 Jan 2022, 10:36
                    0
                    • J jsulm
                      19 Jan 2022, 10:27

                      @feedain Please explain better what you want to achieve.
                      Can you please post JSON document you want to create? So thatb we can actually understand what you are asking.

                      F Offline
                      F Offline
                      feedain
                      wrote on 19 Jan 2022, 10:36 last edited by
                      #10

                      @jsulm said in How to exploit a json file in QT ?:

                      @feedain Please explain better what you want to achieve.
                      Can you please post JSON document you want to create? So thatb we can actually understand what you are asking.

                      Okay so, i have some numbers of port on an arduino to use by state : 0 or 1,
                      Example :

                      [
                          {
                                  "Port": n, // n is port starts 1 to 55
                                  "State": e, //e is 0 or 1
                              },
                      ]
                      

                      It's an example but i want to create a JSON like I show you and after that to change easily the port and the state of port
                      I expect i explain it better...

                      J 1 Reply Last reply 19 Jan 2022, 10:42
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 19 Jan 2022, 10:42 last edited by
                        #11

                        Ignoring the array around the object:

                        QJsonObject obj;
                        obj["Port"] = p;
                        obj["State"] = int(state);
                        

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

                        F 1 Reply Last reply 19 Jan 2022, 10:48
                        2
                        • F feedain
                          19 Jan 2022, 10:36

                          @jsulm said in How to exploit a json file in QT ?:

                          @feedain Please explain better what you want to achieve.
                          Can you please post JSON document you want to create? So thatb we can actually understand what you are asking.

                          Okay so, i have some numbers of port on an arduino to use by state : 0 or 1,
                          Example :

                          [
                              {
                                      "Port": n, // n is port starts 1 to 55
                                      "State": e, //e is 0 or 1
                                  },
                          ]
                          

                          It's an example but i want to create a JSON like I show you and after that to change easily the port and the state of port
                          I expect i explain it better...

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 19 Jan 2022, 10:42 last edited by
                          #12

                          @feedain

                          QJsonObject object;
                          pbject.insert("Port", QJsonValue(p));
                          object.insert("Etat", QJsonValue(e));
                          
                          QJsonArray array;
                          array.append(object);
                          
                          QJsonDocument doc(array);
                          qDebug() << doc.toJson();
                          

                          You should really take time to read documentation and take a look at examples like: https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

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

                          F 1 Reply Last reply 19 Jan 2022, 10:58
                          3
                          • Christian EhrlicherC Christian Ehrlicher
                            19 Jan 2022, 10:42

                            Ignoring the array around the object:

                            QJsonObject obj;
                            obj["Port"] = p;
                            obj["State"] = int(state);
                            
                            F Offline
                            F Offline
                            feedain
                            wrote on 19 Jan 2022, 10:48 last edited by
                            #13

                            @Christian-Ehrlicher said in How to exploit a json file in QT ?:

                            Ignoring the array around the object:

                            QJsonObject obj;
                            obj["Port"] = p;
                            obj["State"] = int(state);
                            

                            obj["Port"] = p; mean you create an array Port and a other State ?

                            Christian EhrlicherC 1 Reply Last reply 19 Jan 2022, 10:50
                            0
                            • F feedain
                              19 Jan 2022, 10:48

                              @Christian-Ehrlicher said in How to exploit a json file in QT ?:

                              Ignoring the array around the object:

                              QJsonObject obj;
                              obj["Port"] = p;
                              obj["State"] = int(state);
                              

                              obj["Port"] = p; mean you create an array Port and a other State ?

                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 19 Jan 2022, 10:50 last edited by
                              #14

                              @feedain said in How to exploit a json file in QT ?:

                              obj["Port"] = p; mean you create an array Port and a other State ?

                              ?

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

                              1 Reply Last reply
                              1
                              • J jsulm
                                19 Jan 2022, 10:42

                                @feedain

                                QJsonObject object;
                                pbject.insert("Port", QJsonValue(p));
                                object.insert("Etat", QJsonValue(e));
                                
                                QJsonArray array;
                                array.append(object);
                                
                                QJsonDocument doc(array);
                                qDebug() << doc.toJson();
                                

                                You should really take time to read documentation and take a look at examples like: https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

                                F Offline
                                F Offline
                                feedain
                                wrote on 19 Jan 2022, 10:58 last edited by
                                #15

                                @jsulm said in How to exploit a json file in QT ?:

                                @feedain

                                QJsonObject object;
                                pbject.insert("Port", QJsonValue(p));
                                object.insert("Etat", QJsonValue(e));
                                
                                QJsonArray array;
                                array.append(object);
                                
                                QJsonDocument doc(array);
                                qDebug() << doc.toJson();
                                

                                You should really take time to read documentation and take a look at examples like: https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

                                I look this example but there is some thing that I doesn't understand

                                Christian EhrlicherC 1 Reply Last reply 19 Jan 2022, 11:37
                                0
                                • F feedain
                                  19 Jan 2022, 10:58

                                  @jsulm said in How to exploit a json file in QT ?:

                                  @feedain

                                  QJsonObject object;
                                  pbject.insert("Port", QJsonValue(p));
                                  object.insert("Etat", QJsonValue(e));
                                  
                                  QJsonArray array;
                                  array.append(object);
                                  
                                  QJsonDocument doc(array);
                                  qDebug() << doc.toJson();
                                  

                                  You should really take time to read documentation and take a look at examples like: https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

                                  I look this example but there is some thing that I doesn't understand

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 19 Jan 2022, 11:37 last edited by
                                  #16

                                  @feedain said in How to exploit a json file in QT ?:

                                  is some thing that I doesn't understand

                                  And what exactly is the problem? How should we know it when you don't tell us what you don't understand?

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

                                  F 2 Replies Last reply 19 Jan 2022, 14:00
                                  0
                                  • Christian EhrlicherC Christian Ehrlicher
                                    19 Jan 2022, 11:37

                                    @feedain said in How to exploit a json file in QT ?:

                                    is some thing that I doesn't understand

                                    And what exactly is the problem? How should we know it when you don't tell us what you don't understand?

                                    F Offline
                                    F Offline
                                    feedain
                                    wrote on 19 Jan 2022, 14:00 last edited by
                                    #17
                                    This post is deleted!
                                    1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher
                                      19 Jan 2022, 11:37

                                      @feedain said in How to exploit a json file in QT ?:

                                      is some thing that I doesn't understand

                                      And what exactly is the problem? How should we know it when you don't tell us what you don't understand?

                                      F Offline
                                      F Offline
                                      feedain
                                      wrote on 19 Jan 2022, 14:11 last edited by
                                      #18

                                      @Christian-Ehrlicher said in How to exploit a json file in QT ?:

                                      @feedain said in How to exploit a json file in QT ?:

                                      is some thing that I doesn't understand

                                      And what exactly is the problem? How should we know it when you don't tell us what you don't understand?

                                      i don't understand on this example how he "create" example. For instance : i create a json file of characters of a game so we have "name" "power" "life"
                                      How do i instanciate a new personnage with this json file ?

                                      J J 2 Replies Last reply 19 Jan 2022, 14:18
                                      0
                                      • F feedain
                                        19 Jan 2022, 14:11

                                        @Christian-Ehrlicher said in How to exploit a json file in QT ?:

                                        @feedain said in How to exploit a json file in QT ?:

                                        is some thing that I doesn't understand

                                        And what exactly is the problem? How should we know it when you don't tell us what you don't understand?

                                        i don't understand on this example how he "create" example. For instance : i create a json file of characters of a game so we have "name" "power" "life"
                                        How do i instanciate a new personnage with this json file ?

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 19 Jan 2022, 14:18 last edited by
                                        #19

                                        @feedain said in How to exploit a json file in QT ?:

                                        How do i instanciate a new personnage with this json file ?

                                        Do you mean you want an array of "personnage" objects? That was in the sample code above:

                                        QJsonArray array;
                                        array.append(object);
                                        

                                        You can keep appending new objects you create, to have as many as you want?

                                        Otherwise, not sure what you mean by "a new personnage with this json file". The file contains whatever it contains, if it contains 10 "personnages" you get 10 personnages. The file does not create further objects, other than whatever is in it. You can create more objects in code, e.g. perhaps by copying some existing personnage object you have read from the file, if that is what you want to do.

                                        1 Reply Last reply
                                        0
                                        • F feedain
                                          19 Jan 2022, 14:11

                                          @Christian-Ehrlicher said in How to exploit a json file in QT ?:

                                          @feedain said in How to exploit a json file in QT ?:

                                          is some thing that I doesn't understand

                                          And what exactly is the problem? How should we know it when you don't tell us what you don't understand?

                                          i don't understand on this example how he "create" example. For instance : i create a json file of characters of a game so we have "name" "power" "life"
                                          How do i instanciate a new personnage with this json file ?

                                          J Offline
                                          J Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on 19 Jan 2022, 14:23 last edited by
                                          #20

                                          @feedain You should really be more clear when asking.
                                          "How do i instanciate a new personnage with this json file ?" - do you mean you want to create an instance of a class and initialise its properties from values you read from a JSON file? If so then please tell us what exactly is not clear:

                                          1. How to read JSON file?
                                          2. How to initialise an instance of a class?

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

                                          F 1 Reply Last reply 19 Jan 2022, 14:45
                                          0

                                          5/28

                                          19 Jan 2022, 09:50

                                          topic:navigator.unread, 23
                                          • Login

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