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. Creating json file with subroot
Forum Updated to NodeBB v4.3 + New Features

Creating json file with subroot

Scheduled Pinned Locked Moved Unsolved General and Desktop
jsonrootqt5
13 Posts 3 Posters 1.8k Views 1 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    What exactly did you try? It's a simply QJsonObject which contains an array of QJsonObjects.

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

    D 1 Reply Last reply
    2
    • Christian EhrlicherC Christian Ehrlicher

      What exactly did you try? It's a simply QJsonObject which contains an array of QJsonObjects.

      D Offline
      D Offline
      deleted286
      wrote on last edited by
      #3

      @Christian-Ehrlicher
      I have written a code like this but it does not work as i want

      QJsonArray root;
      QJsonObject route;
      QJsonObject route_pt;
      
        route.insert(" no", 5);
        route.insert("starting pt", 7);
        route.insert("total pt", 3);
        root.append(route);
      
         route_pt.insert("latitude", 5);
         route_pt.insert("longitude", 3);
         route_pt.insert("no", 5);
      
         root.append(rota_nokta);
         QJsonDocument doc;
         doc.setArray(root);
      
      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        You forgot the Plan object

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

        D 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          You forgot the Plan object

          D Offline
          D Offline
          deleted286
          wrote on last edited by
          #5

          @Christian-Ehrlicher it already gives an error in the last line.

          doc.setArray(root);
          
          : no matching function for call to ‘QJsonDocument::setObject(QJsonArray&)’
              doc.setObject(root);
                                ^
          
          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @suslucoder said in Creating json file with subroot:

            QJsonDocument::setObject(QJsonArray&)’

            This is not what you showed us above!

            doc.setArray(root);

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

            D 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @suslucoder said in Creating json file with subroot:

              QJsonDocument::setObject(QJsonArray&)’

              This is not what you showed us above!

              doc.setArray(root);

              D Offline
              D Offline
              deleted286
              wrote on last edited by
              #7

              @Christian-Ehrlicher I mixed my code sorry.
              I fixed my problem. Now it seems like: How should i add plan, and route and pt info headings

              [
                  {
                      "starting pt": 7,
                      "no": 5,
                      "Total pt": 3
                  },
                  {
                      "latitude": 3,
                      "longitude": 5,
                      "no": 5,
              
                  }
              ]
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                I don't understand your question. Already told you that the array is in an object.

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

                D 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  I don't understand your question. Already told you that the array is in an object.

                  D Offline
                  D Offline
                  deleted286
                  wrote on last edited by
                  #9

                  @Christian-Ehrlicher this is what i want to achieve:

                  {
                      "Plan" : [
                          { 
                            "route":
                               {
                               "no" : 1.12,
                               "Starting pt" : 1.19,
                               "Total pt" :5
                           },
                            "pt info":
                               {
                                "latitude" : 30.42,
                                "longitude" : 41.19,
                                "no" :5
                    
                          }
                         } 
                      ]
                  }
                  

                  And here is mine:

                  [
                      {
                          "starting pt": 7,
                          "no": 5,
                          "Total pt": 3
                      },
                      {
                          "latitude": 3,
                          "longitude": 5,
                          "no": 5,
                  
                      }
                  ]
                  

                  They are so different from each other. I didnt understand, how can i set a root name like plan, or route and pt info. This is my question

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @suslucoder said in Creating json file with subroot:

                    This is my question

                    It's a simply QJsonObject which contains an array

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

                    D 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @suslucoder said in Creating json file with subroot:

                      This is my question

                      It's a simply QJsonObject which contains an array

                      D Offline
                      D Offline
                      deleted286
                      wrote on last edited by
                      #11

                      @Christian-Ehrlicher said in Creating json file with subroot:

                      It's a simply QJsonObject which contains an array

                      This sentence helped me a lot (!)

                      KroMignonK 1 Reply Last reply
                      0
                      • D deleted286

                        @Christian-Ehrlicher said in Creating json file with subroot:

                        It's a simply QJsonObject which contains an array

                        This sentence helped me a lot (!)

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by KroMignon
                        #12

                        @suslucoder said in Creating json file with subroot:

                        This sentence helped me a lot (!)

                        What is so hard to understand?

                        QJsonObject plan;
                        plan.insert("Plan", root);
                        QJsonDocument doc(plan);
                        qDebug() << QString::fromLatin1(doc.toJson());
                        

                        EDIT: it would be better to rename your QJsonArray object root to planArray and plan to root to better match with your json document structure

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        D 1 Reply Last reply
                        2
                        • KroMignonK KroMignon

                          @suslucoder said in Creating json file with subroot:

                          This sentence helped me a lot (!)

                          What is so hard to understand?

                          QJsonObject plan;
                          plan.insert("Plan", root);
                          QJsonDocument doc(plan);
                          qDebug() << QString::fromLatin1(doc.toJson());
                          

                          EDIT: it would be better to rename your QJsonArray object root to planArray and plan to root to better match with your json document structure

                          D Offline
                          D Offline
                          deleted286
                          wrote on last edited by
                          #13

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