Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create special json format to post
Qt 6.11 is out! See what's new in the release blog

How to create special json format to post

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 9 Posters 7.0k Views 3 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.
  • Z Offline
    Z Offline
    ztencmcp
    wrote on last edited by
    #3

    Hey Bro,

    there's the code, appreciate it.

    QJsonObject json;
    json.insert("username", "admin");
    json.insert("password", "admin");
    json.insert("content", "hello world");
    QJsonDocument document;
    document.setObject(json);
    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
    QString json_str(byte_array);
    
    JonBJ Pablo J. RoginaP 2 Replies Last reply
    0
    • Z ztencmcp

      Hey Bro,

      there's the code, appreciate it.

      QJsonObject json;
      json.insert("username", "admin");
      json.insert("password", "admin");
      json.insert("content", "hello world");
      QJsonDocument document;
      document.setObject(json);
      QByteArray byte_array = document.toJson(QJsonDocument::Compact);
      QString json_str(byte_array);
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #4

      @ztencmcp said in How to create special json format to post:

      QString json_str(byte_array);

      isn't that what puts the double-quotes around the whole thing, which is what (I think) you say you so not want? If the server wants an object, which do you convert it to a (JSON) string? (Note that I am not a JSON expert!)

      Z 1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #5

        Everything seems fine, why do you think it's wrong?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        Z 1 Reply Last reply
        0
        • Z ztencmcp

          Hey Bro,

          there's the code, appreciate it.

          QJsonObject json;
          json.insert("username", "admin");
          json.insert("password", "admin");
          json.insert("content", "hello world");
          QJsonDocument document;
          document.setObject(json);
          QByteArray byte_array = document.toJson(QJsonDocument::Compact);
          QString json_str(byte_array);
          
          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #6

          @ztencmcp you may want to check/validate your JSON data with jsonlint

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • JonBJ JonB

            @ztencmcp said in How to create special json format to post:

            QString json_str(byte_array);

            isn't that what puts the double-quotes around the whole thing, which is what (I think) you say you so not want? If the server wants an object, which do you convert it to a (JSON) string? (Note that I am not a JSON expert!)

            Z Offline
            Z Offline
            ztencmcp
            wrote on last edited by
            #7

            @JonB Thanks, even if i use QByteArray type, the server responses json format error when i post data with QNetworkAccessManager. But if i use CPPRESTSDK, i can create the right json format with web::json::value postdata.serialize(), the right format is:

            {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
            

            without qoute before and after brace

            JKSHJ 1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @ztencmcp said in How to create special json format to post:

              {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}

              This is no valid json.

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

              Z 1 Reply Last reply
              0
              • VRoninV VRonin

                Everything seems fine, why do you think it's wrong?

                Z Offline
                Z Offline
                ztencmcp
                wrote on last edited by
                #9

                @VRonin Thanks, if i use CPPRESTSDK, i can create the right json format with web::json::value postdata.serialize(), the right format is:

                {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
                

                without qoute before and after brace

                jsulmJ 1 Reply Last reply
                0
                • Z ztencmcp

                  @VRonin Thanks, if i use CPPRESTSDK, i can create the right json format with web::json::value postdata.serialize(), the right format is:

                  {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
                  

                  without qoute before and after brace

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

                  @ztencmcp said in How to create special json format to post:

                  without qoute before and after brace

                  Can you please explain how you're printing your JSON, so you see these quotes? Do you use qDebug()?
                  Because your "right format" you just posted still does contain the quotes...

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

                  Z 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @ztencmcp said in How to create special json format to post:

                    {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}

                    This is no valid json.

                    Z Offline
                    Z Offline
                    ztencmcp
                    wrote on last edited by
                    #11

                    @Christian-Ehrlicher This is what i used CPPRESTSDK to build the right format with

                    web::json::value postdata.serialize()
                    

                    {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}

                    J.HilkJ JonBJ 2 Replies Last reply
                    0
                    • Z ztencmcp

                      @Christian-Ehrlicher This is what i used CPPRESTSDK to build the right format with

                      web::json::value postdata.serialize()
                      

                      {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #12

                      @ztencmcp can you save the "correct" string to a file and show that one to us?

                      It's still very confusing, what the actual problem is!


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      0
                      • Z ztencmcp

                        @JonB Thanks, even if i use QByteArray type, the server responses json format error when i post data with QNetworkAccessManager. But if i use CPPRESTSDK, i can create the right json format with web::json::value postdata.serialize(), the right format is:

                        {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
                        

                        without qoute before and after brace

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #13

                        @ztencmcp said in How to create special json format to post:

                        the server responses json format error when i post data with QNetworkAccessManager.

                        How do you post? Please show the code from document.setObject(json); to QNetworkAccessManager::post()

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        Z 1 Reply Last reply
                        0
                        • Z ztencmcp

                          @Christian-Ehrlicher This is what i used CPPRESTSDK to build the right format with

                          web::json::value postdata.serialize()
                          

                          {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #14

                          @ztencmcp
                          I don't know, I'm not a JSON expert, but like @Christian-Ehrlicher I thought the JSON you show would not be valid. Look at this bit:

                          /"username/": /"admin/"
                          

                          You have a / outside the quoted string for both the property name & value. Is that really correct??

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @ztencmcp said in How to create special json format to post:

                            without qoute before and after brace

                            Can you please explain how you're printing your JSON, so you see these quotes? Do you use qDebug()?
                            Because your "right format" you just posted still does contain the quotes...

                            Z Offline
                            Z Offline
                            ztencmcp
                            wrote on last edited by
                            #15

                            @jsulm(1574328729935.jpg

                            you can see the pic, the right one i created it with cpprestsdk web::json::value serialize() fucntion, the wrong one i created it with qt5 QJsonObject。http server only accepts the right one。

                            KroMignonK JonBJ 2 Replies Last reply
                            0
                            • Z ztencmcp

                              @jsulm(1574328729935.jpg

                              you can see the pic, the right one i created it with cpprestsdk web::json::value serialize() fucntion, the wrong one i created it with qt5 QJsonObject。http server only accepts the right one。

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

                              @ztencmcp This does not really answer the question, please show the C++ code you have used to send the JSON string to your server.
                              Perhaps you can capture the TCP stream with WireShark to see what is really transmitted to the web server.

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

                              Z 1 Reply Last reply
                              0
                              • JKSHJ JKSH

                                @ztencmcp said in How to create special json format to post:

                                the server responses json format error when i post data with QNetworkAccessManager.

                                How do you post? Please show the code from document.setObject(json); to QNetworkAccessManager::post()

                                Z Offline
                                Z Offline
                                ztencmcp
                                wrote on last edited by
                                #17

                                @JKSH 1574329362366.jpg
                                here's the code. Thanks.

                                And i used cpprestsdk to do the same thing and it's right.

                                1574329499715.jpg

                                KroMignonK 1 Reply Last reply
                                0
                                • J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #18

                                  @ztencmcp

                                  When you look at the content of the QByteArray
                                  aa311749-b43b-48f9-9f31-d5045e9efcc3-image.png

                                  You'll see, there is no " at the beginning or at the end. The Qt creates the same "JSON string" as cpprestsdk

                                  the error is somewhere else.


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  0
                                  • Z ztencmcp

                                    @jsulm(1574328729935.jpg

                                    you can see the pic, the right one i created it with cpprestsdk web::json::value serialize() fucntion, the wrong one i created it with qt5 QJsonObject。http server only accepts the right one。

                                    JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by
                                    #19

                                    @ztencmcp
                                    Can we please be clear about one thing: in your first code it used /", in the stuff you are pasting now it uses \". Are you saying that in JSON one can use either / or \ as the "escape" character??

                                    1 Reply Last reply
                                    0
                                    • KroMignonK KroMignon

                                      @ztencmcp This does not really answer the question, please show the C++ code you have used to send the JSON string to your server.
                                      Perhaps you can capture the TCP stream with WireShark to see what is really transmitted to the web server.

                                      Z Offline
                                      Z Offline
                                      ztencmcp
                                      wrote on last edited by
                                      #20

                                      @KroMignon Thanks for advice, pls see the pics below.

                                      1 Reply Last reply
                                      0
                                      • Z ztencmcp

                                        @JKSH 1574329362366.jpg
                                        here's the code. Thanks.

                                        And i used cpprestsdk to do the same thing and it's right.

                                        1574329499715.jpg

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

                                        @ztencmcp You did NOT specify in your POST request the format!

                                        QNetworkRequest request(QUrl("https://example.com"));
                                        request.setRawHeader("User-Agent", "My app name v0.1");
                                        request.setRawHeader("X-Custom-User-Agent", "My app name v0.1");
                                        request.setRawHeader("Content-Type", "application/json");
                                        request.setRawHeader("Content-Length", postdata.size());
                                        manager->post(request, postdata);
                                        

                                        or

                                        QNetworkRequest request(QUrl("https://example.com"));
                                        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
                                        manager->post(request, postdata);
                                        

                                        Should do the job!

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

                                        Z 1 Reply Last reply
                                        3
                                        • KroMignonK KroMignon

                                          @ztencmcp You did NOT specify in your POST request the format!

                                          QNetworkRequest request(QUrl("https://example.com"));
                                          request.setRawHeader("User-Agent", "My app name v0.1");
                                          request.setRawHeader("X-Custom-User-Agent", "My app name v0.1");
                                          request.setRawHeader("Content-Type", "application/json");
                                          request.setRawHeader("Content-Length", postdata.size());
                                          manager->post(request, postdata);
                                          

                                          or

                                          QNetworkRequest request(QUrl("https://example.com"));
                                          request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
                                          manager->post(request, postdata);
                                          

                                          Should do the job!

                                          Z Offline
                                          Z Offline
                                          ztencmcp
                                          wrote on last edited by
                                          #22

                                          @KroMignon Thanks, let me try.

                                          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
                                          • Unsolved