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. Create JSON-File
QtWS25 Last Chance

Create JSON-File

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.1k 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.
  • N Offline
    N Offline
    NotYourFan
    wrote on last edited by
    #1

    Hey,

    iam trying to create in QT5 a JSON-File that Looks like:

    {
      "ConfigFile": [
        {
          "name": "Car",
          "valueName": "CarValue",
          "actual": {
            "actual": 140
          },
          "upper": {
            "actual": 120.1
          },
          "lower": {
            "actual": 2
          }
        },
        {
          "name": "Car2",
          "valueName": "CarValue2",
          "actual": {
            "actual": 1.2
          },
          "upper": {
            "actual": 22.1
          },
          "lower": {
            "actual": 5
          }
        }
      ]
    }
    

    I try to created with QJsonArray , QJsonObject and QJsonDocument.

    jsulmJ 1 Reply Last reply
    0
    • N NotYourFan

      Hey,

      iam trying to create in QT5 a JSON-File that Looks like:

      {
        "ConfigFile": [
          {
            "name": "Car",
            "valueName": "CarValue",
            "actual": {
              "actual": 140
            },
            "upper": {
              "actual": 120.1
            },
            "lower": {
              "actual": 2
            }
          },
          {
            "name": "Car2",
            "valueName": "CarValue2",
            "actual": {
              "actual": 1.2
            },
            "upper": {
              "actual": 22.1
            },
            "lower": {
              "actual": 5
            }
          }
        ]
      }
      

      I try to created with QJsonArray , QJsonObject and QJsonDocument.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @NotYourFan What's your question?

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

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NotYourFan
        wrote on last edited by
        #3

        how can i create a JSON-File with this structure ?

        jsulmJ 1 Reply Last reply
        0
        • N NotYourFan

          how can i create a JSON-File with this structure ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @NotYourFan Using the classes you mentioned.
          What exactly is the problem?

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

          1 Reply Last reply
          3
          • N Offline
            N Offline
            NotYourFan
            wrote on last edited by
            #5

            i dont know how to get this structure with QT.
            I want to get later a file with exactly the structure like at the top.

            jsulmJ 1 Reply Last reply
            0
            • N NotYourFan

              i dont know how to get this structure with QT.
              I want to get later a file with exactly the structure like at the top.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @NotYourFan Take a look at this example: https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

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

              1 Reply Last reply
              5
              • N Offline
                N Offline
                NotYourFan
                wrote on last edited by
                #7

                okay, so now i cant insert into a JsonObject:

                QJsonObject object;
                object.insert("name", hgvc->value)
                

                how can i insert a String and a double into a Object??

                Gojir4G 1 Reply Last reply
                0
                • N NotYourFan

                  okay, so now i cant insert into a JsonObject:

                  QJsonObject object;
                  object.insert("name", hgvc->value)
                  

                  how can i insert a String and a double into a Object??

                  Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by
                  #8

                  @NotYourFan Hi,
                  Just use the same way to insert string or double values

                  QJsonObject json;
                  json["string"] = "Hello World";
                  json["double"] = 1.234
                  json["int"] = 1234
                  QJsonArray a;
                  a.append(1);
                  a.append(2);
                  a.append(3);
                  a.append(4);
                  json["array"] = a;
                  QJsonObject child;
                  child["string"] = "Hello World";
                  child["double"] = 1.234;
                  child["int"] = 1234;
                  json["child_object"] = child;
                  
                  //Retrieve values
                  QString str = json["string"].toString();
                  double d = json["double"].toDouble();
                  int i = json["int"].toInt();
                  QJsonArray a = json["array"].toArray();
                  for(int ai = 0; ai < a.size(); ai++){
                      int value = a.at(i).toInt();
                  }
                  QJsonObject child = json["child_object"].toObject();
                  QString strChild = child["string"].toString();
                  double dChild = child["double"].toDouble();
                  int iChild = child["int"].toInt();
                  
                  
                  
                  1 Reply Last reply
                  2
                  • N Offline
                    N Offline
                    NotYourFan
                    wrote on last edited by
                    #9

                    Hey @Gojir4 thank you for your answer :)

                    i have a Problem with the Depth.

                    {
                      "ConfigFile": [
                        {
                          "name": "Car",
                          "valueName": "CarValue",
                          "actual": {
                            "actual": 140
                          },
                          "upper": {
                            "actual": 120.1
                          },
                          "lower": {
                            "actual": 2
                          }
                        },
                        {
                          "name": "Car2",
                          "valueName": "CarValue2",
                          "actual": {
                            "actual": 1.2
                          },
                          "upper": {
                            "actual": 22.1
                          },
                          "lower": {
                            "actual": 5
                          }
                        }
                      ]
                    }
                    

                    do you have a explcite exmaple?
                    Thank You !!

                    1 Reply Last reply
                    0

                    • Login

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