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
QtWS25 Last Chance

Creating json file with subroot

Scheduled Pinned Locked Moved Unsolved General and Desktop
jsonrootqt5
13 Posts 3 Posters 1.4k 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.
  • D Offline
    D Offline
    deleted286
    wrote on 5 Feb 2021, 08:02 last edited by
    #1

    How can i create such a json file? I didnt found any information like subroot etc.

    {
        "Plan" : [
            { 
              "route":
                 {
                 "no" : 1.12,
                 "Starting pt" : 1.19,
                 "Total pt" :5
             },
              "pt info":
                 {
                  "latitude" : 30.42,
                  "longitude" : 41.19,
                  "no" :5
      
            }
           } 
        ]
    }
    
    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 5 Feb 2021, 08:07 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 5 Feb 2021, 08:12
      2
      • C Christian Ehrlicher
        5 Feb 2021, 08:07

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

        D Offline
        D Offline
        deleted286
        wrote on 5 Feb 2021, 08:12 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
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 5 Feb 2021, 08:21 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 5 Feb 2021, 08:23
          1
          • C Christian Ehrlicher
            5 Feb 2021, 08:21

            You forgot the Plan object

            D Offline
            D Offline
            deleted286
            wrote on 5 Feb 2021, 08:23 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
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 5 Feb 2021, 08:24 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 5 Feb 2021, 08:33
              1
              • C Christian Ehrlicher
                5 Feb 2021, 08:24

                @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 5 Feb 2021, 08:33 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
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 5 Feb 2021, 08:34 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 5 Feb 2021, 08:37
                  1
                  • C Christian Ehrlicher
                    5 Feb 2021, 08:34

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

                    D Offline
                    D Offline
                    deleted286
                    wrote on 5 Feb 2021, 08:37 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
                    • C Online
                      C Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 5 Feb 2021, 08:38 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 5 Feb 2021, 08:39
                      0
                      • C Christian Ehrlicher
                        5 Feb 2021, 08:38

                        @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 5 Feb 2021, 08:39 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 (!)

                        K 1 Reply Last reply 5 Feb 2021, 08:46
                        0
                        • D deleted286
                          5 Feb 2021, 08:39

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

                          It's a simply QJsonObject which contains an array

                          This sentence helped me a lot (!)

                          K Offline
                          K Offline
                          KroMignon
                          wrote on 5 Feb 2021, 08:46 last edited by KroMignon 2 May 2021, 08:50
                          #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 5 Feb 2021, 08:56
                          2
                          • K KroMignon
                            5 Feb 2021, 08:46

                            @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 5 Feb 2021, 08:56 last edited by
                            #13

                            @KroMignon thank you

                            1 Reply Last reply
                            0

                            9/13

                            5 Feb 2021, 08:37

                            • Login

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