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. Initialising QJsonObject with JSON
QtWS25 Last Chance

Initialising QJsonObject with JSON

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 1.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.
  • S Offline
    S Offline
    SPlatten
    wrote on 10 Dec 2020, 07:50 last edited by SPlatten 12 Oct 2020, 07:51
    #1

    I have the following JSON:

    {"commands":[{"check":"a"
                                     ,"command":"read"
                                        ,"mode":"binary"
                                      ,"length":1
                                        ,"type":"char"}
                                  ,{"byteorder":"0,1"
                                       ,"check":32767
                                     ,"command":"read"
                                      ,"length":2
                                        ,"mode":"binary"
                                        ,"type":"short"}
                                  ,{"byteorder":"0,1,2,3"
                                     ,"command":"read"
                                        ,"mode":"binary"
                                      ,"length":4
                                        ,"type":"int"
                                       ,"check":2147483647}
                                  ,{"byteorder":"0,1,2,3,4,5,6,7"
                                       ,"check":9223372036854775807
                                     ,"command":"read"
                                      ,"length":8
                                        ,"mode":"binary"
                                        ,"type":"long"}
                                  ,{"byteorder":"0,1,2,3,4,5,6,7"
                                       ,"check":3.4028234663852886e+38
                                     ,"command":"read"
                                      ,"length":8
                                        ,"mode":"binary"
                                        ,"type":"float"}
                                  ,{"byteorder":"0,1,2,3,4,5,6,7"
                                       ,"check":1.7976931348623157e+308
                                     ,"command":"read"
                                      ,"length":8
                                        ,"mode":"binary"
                                        ,"type":"double"}
                                      ,{"check":"abcdefghijklmnopqrstuvwxyz"
                                     ,"command":"read"
                                      ,"length":27
                                        ,"mode":"binary"
                                        ,"type":"string"}
                                      ,{"check":"b"
                                     ,"command":"read"
                                      ,"length":1
                                        ,"mode":"binary"
                                        ,"type":"uchar"}
                                  ,{"byteorder":"0,1"
                                       ,"check":65535
                                     ,"command":"read"
                                      ,"length":2
                                        ,"mode":"binary"
                                        ,"type":"ushort"}
                                  ,{"byteorder":"0,1,2,3"
                                       ,"check":4294967295
                                     ,"command":"read"
                                      ,"length":4
                                        ,"mode":"binary"
                                        ,"type":"uint"}
                                  ,{"byteorder":"0,1,2,3,4,5,6,7"
                                       ,"check":18446744073709551615
                                     ,"command":"read"
                                      ,"length":8
                                        ,"mode":"binary"
                                        ,"type":"ulong"}
                                    ,{"command":"find"
                                        ,"from":"start"
                                     ,"pattern":"JKL"
                                    ,"skipover":false}
                                    ,{"command":"read"
                                      ,"length":3
                                        ,"mode":"text"}
                                    ,{"command":"read"
                                      ,"length":4
                                        ,"mode":"text"}
                                    ,{"command":"read"
                                      ,"length":5
                                        ,"mode":"text"
                                    ,"position":13}
                                    ,{"command":"tell"}]
                              ,"file":"~/XMLMPAM/config/test.dat"
                            ,"module":"mdFileIO"}
    

    I've used an online validation tool:

    https://jsonlint.com
    

    The above is valid JSON. How do I initialise an instance of QJsonObject with this? Currently I get:

    expected '}'
    

    to the right of the first line when I try:

    QJsonObject objJSON   {"commands":[{"check":"a"
                                     ,"command":"read"
                                        ,"mode":"binary"
                                      ,"length":1
                                        ,"type":"char"}
                                  ,{"byteorder":"0,1"
                                       ,"check":32767
                                     ,"command":"read"
                                      ,"length":2
                                        ,"mode":"binary"
                                        ,"type":"short"}
                                  ,{"byteorder":"0,1,2,3"
                                     ,"command":"read"
                                        ,"mode":"binary"
                                      ,"length":4
                                        ,"type":"int"
                                       ,"check":2147483647}
                                  ,{"byteorder":"0,1,2,3,4,5,6,7"
                                       ,"check":9223372036854775807
                                     ,"command":"read"
                                      ,"length":8
                                        ,"mode":"binary"
                                        ,"type":"long"}
                                  ,{"byteorder":"0,1,2,3,4,5,6,7"
                                       ,"check":3.4028234663852886e+38
                                     ,"command":"read"
                                      ,"length":8
                                        ,"mode":"binary"
                                        ,"type":"float"}
                                  ,{"byteorder":"0,1,2,3,4,5,6,7"
                                       ,"check":1.7976931348623157e+308
                                     ,"command":"read"
                                      ,"length":8
                                        ,"mode":"binary"
                                        ,"type":"double"}
                                      ,{"check":"abcdefghijklmnopqrstuvwxyz"
                                     ,"command":"read"
                                      ,"length":27
                                        ,"mode":"binary"
                                        ,"type":"string"}
                                      ,{"check":"b"
                                     ,"command":"read"
                                      ,"length":1
                                        ,"mode":"binary"
                                        ,"type":"uchar"}
                                  ,{"byteorder":"0,1"
                                       ,"check":65535
                                     ,"command":"read"
                                      ,"length":2
                                        ,"mode":"binary"
                                        ,"type":"ushort"}
                                  ,{"byteorder":"0,1,2,3"
                                       ,"check":4294967295
                                     ,"command":"read"
                                      ,"length":4
                                        ,"mode":"binary"
                                        ,"type":"uint"}
                                  ,{"byteorder":"0,1,2,3,4,5,6,7"
                                       ,"check":18446744073709551615
                                     ,"command":"read"
                                      ,"length":8
                                        ,"mode":"binary"
                                        ,"type":"ulong"}
                                    ,{"command":"find"
                                        ,"from":"start"
                                     ,"pattern":"JKL"
                                    ,"skipover":false}
                                    ,{"command":"read"
                                      ,"length":3
                                        ,"mode":"text"}
                                    ,{"command":"read"
                                      ,"length":4
                                        ,"mode":"text"}
                                    ,{"command":"read"
                                      ,"length":5
                                        ,"mode":"text"
                                    ,"position":13}
                                    ,{"command":"tell"}]
                              ,"file":"~/XMLMPAM/config/test.dat"
                            ,"module":"mdFileIO"};
    

    Kind Regards,
    Sy

    K J 2 Replies Last reply 10 Dec 2020, 08:02
    0
    • S SPlatten
      10 Dec 2020, 08:29

      @KroMignon , this is what I have now:

      QString strJSON =    "{\"commands\":[{\"check\":\"a\""
                                        ",\"command\":\"read\""
                                           ",\"mode\":\"binary\""
                                         ",\"length\":1"
                                           ",\"type\":\"char\"}"
                                     ",{\"byteorder\":\"0,1\""
                                          ",\"check\":32767"
                                       ",\"command\":\"read\""
                                        ",\"length\":2"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"short\"}"
                                    ",{\"byteorder\":\"0,1,2,3\""
                                         ",\"check\":2147483647"
                                       ",\"command\":\"read\""
                                        ",\"length\":4"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"int\"}"
                                    ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                         ",\"check\":9223372036854775807"
                                       ",\"command\":\"read\""
                                        ",\"length\":8"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"long\"}"
                                    ",{\"byteorder\":\"0,1,2,3\""
                                         ",\"check\":3.4028234663852886e+38"
                                       ",\"command\":\"read\""
                                        ",\"length\":4"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"float\"}"
                                    ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                         ",\"check\":1.7976931348623157e+308"
                                       ",\"command\":\"read\""
                                        ",\"length\":8"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"double\"}"
                                        ",{\"check\":\"abcdefghijklmnopqrstuvwxyz\""
                                       ",\"command\":\"read\""
                                        ",\"length\":27"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"string\"}"
                                        ",{\"check\":\"b\""
                                       ",\"command\":\"read\""
                                        ",\"length\":1"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"uchar\"}"
                                    ",{\"byteorder\":\"0,1\""
                                         ",\"check\":65535"
                                       ",\"command\":\"read\""
                                        ",\"length\":2"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"ushort\"}"
                                    ",{\"byteorder\":\"0,1,2,3\""
                                         ",\"check\":4294967295"
                                       ",\"command\":\"read\""
                                        ",\"length\":4"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"uint\"}"
                                    ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                         ",\"check\":18446744073709551615"
                                       ",\"command\":\"read\""
                                        ",\"length\":8"
                                          ",\"mode\":\"binary\""
                                          ",\"type\":\"ulong\"}"
                                      ",{\"command\":\"find\""
                                          ",\"from\":\"start\""
                                       ",\"pattern\":\"JKL\""
                                      ",\"skipover\":false}"
                                      ",{\"command\":\"read\""
                                        ",\"length\":3"
                                          ",\"mode\":\"text\"}"
                                      ",{\"command\":\"read\""
                                        ",\"length\":4"
                                          ",\"mode\":\"text\"}"
                                      ",{\"command\":\"read\""
                                        ",\"length\":5"
                                          ",\"mode\":\"text\""
                                      ",\"position\":13}"
                                      ",{\"command\":\"tell\"}]"
                                ",\"file\":\"~/XMLMPAM/config/test.dat\""
                              ",\"module\":\"mdFileIO\"};";
      QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());
      

      This is all just to test functionality in my application.

      K Offline
      K Offline
      KroMignon
      wrote on 10 Dec 2020, 08:34 last edited by KroMignon 12 Oct 2020, 08:36
      #8

      @SPlatten said in Initialising QJsonObject with JSON:

      QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());

      I would change this to QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8());
      Because, as written in documentation, and UTF-8 encoded JSON document is awaited:

      [static]QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)

      Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.

      But I do not understand, do it work for you or do you still have trouble?

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

      S 1 Reply Last reply 10 Dec 2020, 08:39
      1
      • S SPlatten
        10 Dec 2020, 07:50

        I have the following JSON:

        {"commands":[{"check":"a"
                                         ,"command":"read"
                                            ,"mode":"binary"
                                          ,"length":1
                                            ,"type":"char"}
                                      ,{"byteorder":"0,1"
                                           ,"check":32767
                                         ,"command":"read"
                                          ,"length":2
                                            ,"mode":"binary"
                                            ,"type":"short"}
                                      ,{"byteorder":"0,1,2,3"
                                         ,"command":"read"
                                            ,"mode":"binary"
                                          ,"length":4
                                            ,"type":"int"
                                           ,"check":2147483647}
                                      ,{"byteorder":"0,1,2,3,4,5,6,7"
                                           ,"check":9223372036854775807
                                         ,"command":"read"
                                          ,"length":8
                                            ,"mode":"binary"
                                            ,"type":"long"}
                                      ,{"byteorder":"0,1,2,3,4,5,6,7"
                                           ,"check":3.4028234663852886e+38
                                         ,"command":"read"
                                          ,"length":8
                                            ,"mode":"binary"
                                            ,"type":"float"}
                                      ,{"byteorder":"0,1,2,3,4,5,6,7"
                                           ,"check":1.7976931348623157e+308
                                         ,"command":"read"
                                          ,"length":8
                                            ,"mode":"binary"
                                            ,"type":"double"}
                                          ,{"check":"abcdefghijklmnopqrstuvwxyz"
                                         ,"command":"read"
                                          ,"length":27
                                            ,"mode":"binary"
                                            ,"type":"string"}
                                          ,{"check":"b"
                                         ,"command":"read"
                                          ,"length":1
                                            ,"mode":"binary"
                                            ,"type":"uchar"}
                                      ,{"byteorder":"0,1"
                                           ,"check":65535
                                         ,"command":"read"
                                          ,"length":2
                                            ,"mode":"binary"
                                            ,"type":"ushort"}
                                      ,{"byteorder":"0,1,2,3"
                                           ,"check":4294967295
                                         ,"command":"read"
                                          ,"length":4
                                            ,"mode":"binary"
                                            ,"type":"uint"}
                                      ,{"byteorder":"0,1,2,3,4,5,6,7"
                                           ,"check":18446744073709551615
                                         ,"command":"read"
                                          ,"length":8
                                            ,"mode":"binary"
                                            ,"type":"ulong"}
                                        ,{"command":"find"
                                            ,"from":"start"
                                         ,"pattern":"JKL"
                                        ,"skipover":false}
                                        ,{"command":"read"
                                          ,"length":3
                                            ,"mode":"text"}
                                        ,{"command":"read"
                                          ,"length":4
                                            ,"mode":"text"}
                                        ,{"command":"read"
                                          ,"length":5
                                            ,"mode":"text"
                                        ,"position":13}
                                        ,{"command":"tell"}]
                                  ,"file":"~/XMLMPAM/config/test.dat"
                                ,"module":"mdFileIO"}
        

        I've used an online validation tool:

        https://jsonlint.com
        

        The above is valid JSON. How do I initialise an instance of QJsonObject with this? Currently I get:

        expected '}'
        

        to the right of the first line when I try:

        QJsonObject objJSON   {"commands":[{"check":"a"
                                         ,"command":"read"
                                            ,"mode":"binary"
                                          ,"length":1
                                            ,"type":"char"}
                                      ,{"byteorder":"0,1"
                                           ,"check":32767
                                         ,"command":"read"
                                          ,"length":2
                                            ,"mode":"binary"
                                            ,"type":"short"}
                                      ,{"byteorder":"0,1,2,3"
                                         ,"command":"read"
                                            ,"mode":"binary"
                                          ,"length":4
                                            ,"type":"int"
                                           ,"check":2147483647}
                                      ,{"byteorder":"0,1,2,3,4,5,6,7"
                                           ,"check":9223372036854775807
                                         ,"command":"read"
                                          ,"length":8
                                            ,"mode":"binary"
                                            ,"type":"long"}
                                      ,{"byteorder":"0,1,2,3,4,5,6,7"
                                           ,"check":3.4028234663852886e+38
                                         ,"command":"read"
                                          ,"length":8
                                            ,"mode":"binary"
                                            ,"type":"float"}
                                      ,{"byteorder":"0,1,2,3,4,5,6,7"
                                           ,"check":1.7976931348623157e+308
                                         ,"command":"read"
                                          ,"length":8
                                            ,"mode":"binary"
                                            ,"type":"double"}
                                          ,{"check":"abcdefghijklmnopqrstuvwxyz"
                                         ,"command":"read"
                                          ,"length":27
                                            ,"mode":"binary"
                                            ,"type":"string"}
                                          ,{"check":"b"
                                         ,"command":"read"
                                          ,"length":1
                                            ,"mode":"binary"
                                            ,"type":"uchar"}
                                      ,{"byteorder":"0,1"
                                           ,"check":65535
                                         ,"command":"read"
                                          ,"length":2
                                            ,"mode":"binary"
                                            ,"type":"ushort"}
                                      ,{"byteorder":"0,1,2,3"
                                           ,"check":4294967295
                                         ,"command":"read"
                                          ,"length":4
                                            ,"mode":"binary"
                                            ,"type":"uint"}
                                      ,{"byteorder":"0,1,2,3,4,5,6,7"
                                           ,"check":18446744073709551615
                                         ,"command":"read"
                                          ,"length":8
                                            ,"mode":"binary"
                                            ,"type":"ulong"}
                                        ,{"command":"find"
                                            ,"from":"start"
                                         ,"pattern":"JKL"
                                        ,"skipover":false}
                                        ,{"command":"read"
                                          ,"length":3
                                            ,"mode":"text"}
                                        ,{"command":"read"
                                          ,"length":4
                                            ,"mode":"text"}
                                        ,{"command":"read"
                                          ,"length":5
                                            ,"mode":"text"
                                        ,"position":13}
                                        ,{"command":"tell"}]
                                  ,"file":"~/XMLMPAM/config/test.dat"
                                ,"module":"mdFileIO"};
        
        K Offline
        K Offline
        KroMignon
        wrote on 10 Dec 2020, 08:02 last edited by KroMignon 12 Oct 2020, 08:08
        #2

        @SPlatten said in Initialising QJsonObject with JSON:

        QJsonObject

        Parsing JSON should be done with QJsonDocument not QJsonObject:

        QString jsonString;  // load with JSON string
        QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString);
        QJsonObject jsonObject = jsonDoc.object();
        

        Edit: sorry, bad reply, did not read correctly your request!
        Never used QJsonObject initialization with std::initializer_list(), I suppose nested std::initializer_list is not supported.

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

        S 1 Reply Last reply 10 Dec 2020, 08:07
        1
        • K KroMignon
          10 Dec 2020, 08:02

          @SPlatten said in Initialising QJsonObject with JSON:

          QJsonObject

          Parsing JSON should be done with QJsonDocument not QJsonObject:

          QString jsonString;  // load with JSON string
          QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString);
          QJsonObject jsonObject = jsonDoc.object();
          

          Edit: sorry, bad reply, did not read correctly your request!
          Never used QJsonObject initialization with std::initializer_list(), I suppose nested std::initializer_list is not supported.

          S Offline
          S Offline
          SPlatten
          wrote on 10 Dec 2020, 08:07 last edited by
          #3

          @KroMignon , thank you, I'm building it up now with:

          QJsonObject objCmd, objJSON;
          QJsonArray aryCmds;
          objCmd.insert(    "check", "a");
          objCmd.insert(  "command", "read");
          objCmd.insert(   "length", 1);
          objCmd.insert(     "mode", "binary");
          objCmd.insert(     "type", "char");
          aryCmds.append(objCmd);
          objCmd.insert("byteorder", "0,1");
          objCmd.insert(    "check", "32767");
          objCmd.insert(  "command", "read");
          objCmd.insert(   "length", 2);
          objCmd.insert(     "mode", "binary");
          objCmd.insert(     "type", "short");
          aryCmds.append(objCmd);
          
          objJSON.insert("commands", aryCmds);
          

          Still a way to go...

          Kind Regards,
          Sy

          J K 2 Replies Last reply 10 Dec 2020, 08:09
          0
          • S SPlatten
            10 Dec 2020, 07:50

            I have the following JSON:

            {"commands":[{"check":"a"
                                             ,"command":"read"
                                                ,"mode":"binary"
                                              ,"length":1
                                                ,"type":"char"}
                                          ,{"byteorder":"0,1"
                                               ,"check":32767
                                             ,"command":"read"
                                              ,"length":2
                                                ,"mode":"binary"
                                                ,"type":"short"}
                                          ,{"byteorder":"0,1,2,3"
                                             ,"command":"read"
                                                ,"mode":"binary"
                                              ,"length":4
                                                ,"type":"int"
                                               ,"check":2147483647}
                                          ,{"byteorder":"0,1,2,3,4,5,6,7"
                                               ,"check":9223372036854775807
                                             ,"command":"read"
                                              ,"length":8
                                                ,"mode":"binary"
                                                ,"type":"long"}
                                          ,{"byteorder":"0,1,2,3,4,5,6,7"
                                               ,"check":3.4028234663852886e+38
                                             ,"command":"read"
                                              ,"length":8
                                                ,"mode":"binary"
                                                ,"type":"float"}
                                          ,{"byteorder":"0,1,2,3,4,5,6,7"
                                               ,"check":1.7976931348623157e+308
                                             ,"command":"read"
                                              ,"length":8
                                                ,"mode":"binary"
                                                ,"type":"double"}
                                              ,{"check":"abcdefghijklmnopqrstuvwxyz"
                                             ,"command":"read"
                                              ,"length":27
                                                ,"mode":"binary"
                                                ,"type":"string"}
                                              ,{"check":"b"
                                             ,"command":"read"
                                              ,"length":1
                                                ,"mode":"binary"
                                                ,"type":"uchar"}
                                          ,{"byteorder":"0,1"
                                               ,"check":65535
                                             ,"command":"read"
                                              ,"length":2
                                                ,"mode":"binary"
                                                ,"type":"ushort"}
                                          ,{"byteorder":"0,1,2,3"
                                               ,"check":4294967295
                                             ,"command":"read"
                                              ,"length":4
                                                ,"mode":"binary"
                                                ,"type":"uint"}
                                          ,{"byteorder":"0,1,2,3,4,5,6,7"
                                               ,"check":18446744073709551615
                                             ,"command":"read"
                                              ,"length":8
                                                ,"mode":"binary"
                                                ,"type":"ulong"}
                                            ,{"command":"find"
                                                ,"from":"start"
                                             ,"pattern":"JKL"
                                            ,"skipover":false}
                                            ,{"command":"read"
                                              ,"length":3
                                                ,"mode":"text"}
                                            ,{"command":"read"
                                              ,"length":4
                                                ,"mode":"text"}
                                            ,{"command":"read"
                                              ,"length":5
                                                ,"mode":"text"
                                            ,"position":13}
                                            ,{"command":"tell"}]
                                      ,"file":"~/XMLMPAM/config/test.dat"
                                    ,"module":"mdFileIO"}
            

            I've used an online validation tool:

            https://jsonlint.com
            

            The above is valid JSON. How do I initialise an instance of QJsonObject with this? Currently I get:

            expected '}'
            

            to the right of the first line when I try:

            QJsonObject objJSON   {"commands":[{"check":"a"
                                             ,"command":"read"
                                                ,"mode":"binary"
                                              ,"length":1
                                                ,"type":"char"}
                                          ,{"byteorder":"0,1"
                                               ,"check":32767
                                             ,"command":"read"
                                              ,"length":2
                                                ,"mode":"binary"
                                                ,"type":"short"}
                                          ,{"byteorder":"0,1,2,3"
                                             ,"command":"read"
                                                ,"mode":"binary"
                                              ,"length":4
                                                ,"type":"int"
                                               ,"check":2147483647}
                                          ,{"byteorder":"0,1,2,3,4,5,6,7"
                                               ,"check":9223372036854775807
                                             ,"command":"read"
                                              ,"length":8
                                                ,"mode":"binary"
                                                ,"type":"long"}
                                          ,{"byteorder":"0,1,2,3,4,5,6,7"
                                               ,"check":3.4028234663852886e+38
                                             ,"command":"read"
                                              ,"length":8
                                                ,"mode":"binary"
                                                ,"type":"float"}
                                          ,{"byteorder":"0,1,2,3,4,5,6,7"
                                               ,"check":1.7976931348623157e+308
                                             ,"command":"read"
                                              ,"length":8
                                                ,"mode":"binary"
                                                ,"type":"double"}
                                              ,{"check":"abcdefghijklmnopqrstuvwxyz"
                                             ,"command":"read"
                                              ,"length":27
                                                ,"mode":"binary"
                                                ,"type":"string"}
                                              ,{"check":"b"
                                             ,"command":"read"
                                              ,"length":1
                                                ,"mode":"binary"
                                                ,"type":"uchar"}
                                          ,{"byteorder":"0,1"
                                               ,"check":65535
                                             ,"command":"read"
                                              ,"length":2
                                                ,"mode":"binary"
                                                ,"type":"ushort"}
                                          ,{"byteorder":"0,1,2,3"
                                               ,"check":4294967295
                                             ,"command":"read"
                                              ,"length":4
                                                ,"mode":"binary"
                                                ,"type":"uint"}
                                          ,{"byteorder":"0,1,2,3,4,5,6,7"
                                               ,"check":18446744073709551615
                                             ,"command":"read"
                                              ,"length":8
                                                ,"mode":"binary"
                                                ,"type":"ulong"}
                                            ,{"command":"find"
                                                ,"from":"start"
                                             ,"pattern":"JKL"
                                            ,"skipover":false}
                                            ,{"command":"read"
                                              ,"length":3
                                                ,"mode":"text"}
                                            ,{"command":"read"
                                              ,"length":4
                                                ,"mode":"text"}
                                            ,{"command":"read"
                                              ,"length":5
                                                ,"mode":"text"
                                            ,"position":13}
                                            ,{"command":"tell"}]
                                      ,"file":"~/XMLMPAM/config/test.dat"
                                    ,"module":"mdFileIO"};
            
            J Offline
            J Offline
            JonB
            wrote on 10 Dec 2020, 08:07 last edited by
            #4

            @SPlatten
            Your syntax is wrong because you are mixing JSON syntax ("name" : ...) with C++ syntax.

            I don't know that @KroMignon is right, saying you must go via QJsonObject.

            I was about to tell you to look at https://stackoverflow.com/questions/50647381/qt-jsonobjects-and-initializer-lists-deeper-than-one-level, which gives you a couple of alternative syntaxes for initializing QJsonObject from a literal.

            1 Reply Last reply
            1
            • S SPlatten
              10 Dec 2020, 08:07

              @KroMignon , thank you, I'm building it up now with:

              QJsonObject objCmd, objJSON;
              QJsonArray aryCmds;
              objCmd.insert(    "check", "a");
              objCmd.insert(  "command", "read");
              objCmd.insert(   "length", 1);
              objCmd.insert(     "mode", "binary");
              objCmd.insert(     "type", "char");
              aryCmds.append(objCmd);
              objCmd.insert("byteorder", "0,1");
              objCmd.insert(    "check", "32767");
              objCmd.insert(  "command", "read");
              objCmd.insert(   "length", 2);
              objCmd.insert(     "mode", "binary");
              objCmd.insert(     "type", "short");
              aryCmds.append(objCmd);
              
              objJSON.insert("commands", aryCmds);
              

              Still a way to go...

              J Offline
              J Offline
              JonB
              wrote on 10 Dec 2020, 08:09 last edited by JonB 12 Oct 2020, 08:21
              #5

              @SPlatten
              Your last reply has crossed with my response. You definitely do not want to start breaking your list into hundreds of separate insert() statements, IMHO. Suggest you look again. Either do it as shown in the stackoverflow post (the accepted solution one), or stick to a single string passed to QJsonDocument::fromJson() (the other answer there, which is @KroMignon's way, and perhaps the nicest/least typing).

              1 Reply Last reply
              1
              • S SPlatten
                10 Dec 2020, 08:07

                @KroMignon , thank you, I'm building it up now with:

                QJsonObject objCmd, objJSON;
                QJsonArray aryCmds;
                objCmd.insert(    "check", "a");
                objCmd.insert(  "command", "read");
                objCmd.insert(   "length", 1);
                objCmd.insert(     "mode", "binary");
                objCmd.insert(     "type", "char");
                aryCmds.append(objCmd);
                objCmd.insert("byteorder", "0,1");
                objCmd.insert(    "check", "32767");
                objCmd.insert(  "command", "read");
                objCmd.insert(   "length", 2);
                objCmd.insert(     "mode", "binary");
                objCmd.insert(     "type", "short");
                aryCmds.append(objCmd);
                
                objJSON.insert("commands", aryCmds);
                

                Still a way to go...

                K Offline
                K Offline
                KroMignon
                wrote on 10 Dec 2020, 08:14 last edited by KroMignon 12 Oct 2020, 08:14
                #6

                @SPlatten said in Initialising QJsonObject with JSON:

                Still a way to go...

                Yes, I apologize, I have not right understand your question and replied too fast.
                The problem is that the QJsonObject reclams a std::initializer_list<QPair<QString, QJsonValue>() as parameter.
                But you command is not matching with this type.

                The easiest way is to use QJsonDocument::fromJson(), this could be done from a file or from a string.

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

                S 1 Reply Last reply 10 Dec 2020, 08:29
                1
                • K KroMignon
                  10 Dec 2020, 08:14

                  @SPlatten said in Initialising QJsonObject with JSON:

                  Still a way to go...

                  Yes, I apologize, I have not right understand your question and replied too fast.
                  The problem is that the QJsonObject reclams a std::initializer_list<QPair<QString, QJsonValue>() as parameter.
                  But you command is not matching with this type.

                  The easiest way is to use QJsonDocument::fromJson(), this could be done from a file or from a string.

                  S Offline
                  S Offline
                  SPlatten
                  wrote on 10 Dec 2020, 08:29 last edited by SPlatten 12 Oct 2020, 08:33
                  #7

                  @KroMignon , this is what I have now:

                  QString strJSON =    "{\"commands\":[{\"check\":\"a\""
                                                    ",\"command\":\"read\""
                                                       ",\"mode\":\"binary\""
                                                     ",\"length\":1"
                                                       ",\"type\":\"char\"}"
                                                 ",{\"byteorder\":\"0,1\""
                                                      ",\"check\":32767"
                                                   ",\"command\":\"read\""
                                                    ",\"length\":2"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"short\"}"
                                                ",{\"byteorder\":\"0,1,2,3\""
                                                     ",\"check\":2147483647"
                                                   ",\"command\":\"read\""
                                                    ",\"length\":4"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"int\"}"
                                                ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                                     ",\"check\":9223372036854775807"
                                                   ",\"command\":\"read\""
                                                    ",\"length\":8"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"long\"}"
                                                ",{\"byteorder\":\"0,1,2,3\""
                                                     ",\"check\":3.4028234663852886e+38"
                                                   ",\"command\":\"read\""
                                                    ",\"length\":4"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"float\"}"
                                                ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                                     ",\"check\":1.7976931348623157e+308"
                                                   ",\"command\":\"read\""
                                                    ",\"length\":8"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"double\"}"
                                                    ",{\"check\":\"abcdefghijklmnopqrstuvwxyz\""
                                                   ",\"command\":\"read\""
                                                    ",\"length\":27"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"string\"}"
                                                    ",{\"check\":\"b\""
                                                   ",\"command\":\"read\""
                                                    ",\"length\":1"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"uchar\"}"
                                                ",{\"byteorder\":\"0,1\""
                                                     ",\"check\":65535"
                                                   ",\"command\":\"read\""
                                                    ",\"length\":2"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"ushort\"}"
                                                ",{\"byteorder\":\"0,1,2,3\""
                                                     ",\"check\":4294967295"
                                                   ",\"command\":\"read\""
                                                    ",\"length\":4"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"uint\"}"
                                                ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                                     ",\"check\":18446744073709551615"
                                                   ",\"command\":\"read\""
                                                    ",\"length\":8"
                                                      ",\"mode\":\"binary\""
                                                      ",\"type\":\"ulong\"}"
                                                  ",{\"command\":\"find\""
                                                      ",\"from\":\"start\""
                                                   ",\"pattern\":\"JKL\""
                                                  ",\"skipover\":false}"
                                                  ",{\"command\":\"read\""
                                                    ",\"length\":3"
                                                      ",\"mode\":\"text\"}"
                                                  ",{\"command\":\"read\""
                                                    ",\"length\":4"
                                                      ",\"mode\":\"text\"}"
                                                  ",{\"command\":\"read\""
                                                    ",\"length\":5"
                                                      ",\"mode\":\"text\""
                                                  ",\"position\":13}"
                                                  ",{\"command\":\"tell\"}]"
                                            ",\"file\":\"~/XMLMPAM/config/test.dat\""
                                          ",\"module\":\"mdFileIO\"};";
                  QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());
                  

                  This is all just to test functionality in my application.

                  Kind Regards,
                  Sy

                  K 1 Reply Last reply 10 Dec 2020, 08:34
                  0
                  • S SPlatten
                    10 Dec 2020, 08:29

                    @KroMignon , this is what I have now:

                    QString strJSON =    "{\"commands\":[{\"check\":\"a\""
                                                      ",\"command\":\"read\""
                                                         ",\"mode\":\"binary\""
                                                       ",\"length\":1"
                                                         ",\"type\":\"char\"}"
                                                   ",{\"byteorder\":\"0,1\""
                                                        ",\"check\":32767"
                                                     ",\"command\":\"read\""
                                                      ",\"length\":2"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"short\"}"
                                                  ",{\"byteorder\":\"0,1,2,3\""
                                                       ",\"check\":2147483647"
                                                     ",\"command\":\"read\""
                                                      ",\"length\":4"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"int\"}"
                                                  ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                                       ",\"check\":9223372036854775807"
                                                     ",\"command\":\"read\""
                                                      ",\"length\":8"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"long\"}"
                                                  ",{\"byteorder\":\"0,1,2,3\""
                                                       ",\"check\":3.4028234663852886e+38"
                                                     ",\"command\":\"read\""
                                                      ",\"length\":4"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"float\"}"
                                                  ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                                       ",\"check\":1.7976931348623157e+308"
                                                     ",\"command\":\"read\""
                                                      ",\"length\":8"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"double\"}"
                                                      ",{\"check\":\"abcdefghijklmnopqrstuvwxyz\""
                                                     ",\"command\":\"read\""
                                                      ",\"length\":27"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"string\"}"
                                                      ",{\"check\":\"b\""
                                                     ",\"command\":\"read\""
                                                      ",\"length\":1"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"uchar\"}"
                                                  ",{\"byteorder\":\"0,1\""
                                                       ",\"check\":65535"
                                                     ",\"command\":\"read\""
                                                      ",\"length\":2"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"ushort\"}"
                                                  ",{\"byteorder\":\"0,1,2,3\""
                                                       ",\"check\":4294967295"
                                                     ",\"command\":\"read\""
                                                      ",\"length\":4"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"uint\"}"
                                                  ",{\"byteorder\":\"0,1,2,3,4,5,6,7\""
                                                       ",\"check\":18446744073709551615"
                                                     ",\"command\":\"read\""
                                                      ",\"length\":8"
                                                        ",\"mode\":\"binary\""
                                                        ",\"type\":\"ulong\"}"
                                                    ",{\"command\":\"find\""
                                                        ",\"from\":\"start\""
                                                     ",\"pattern\":\"JKL\""
                                                    ",\"skipover\":false}"
                                                    ",{\"command\":\"read\""
                                                      ",\"length\":3"
                                                        ",\"mode\":\"text\"}"
                                                    ",{\"command\":\"read\""
                                                      ",\"length\":4"
                                                        ",\"mode\":\"text\"}"
                                                    ",{\"command\":\"read\""
                                                      ",\"length\":5"
                                                        ",\"mode\":\"text\""
                                                    ",\"position\":13}"
                                                    ",{\"command\":\"tell\"}]"
                                              ",\"file\":\"~/XMLMPAM/config/test.dat\""
                                            ",\"module\":\"mdFileIO\"};";
                    QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());
                    

                    This is all just to test functionality in my application.

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 10 Dec 2020, 08:34 last edited by KroMignon 12 Oct 2020, 08:36
                    #8

                    @SPlatten said in Initialising QJsonObject with JSON:

                    QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());

                    I would change this to QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8());
                    Because, as written in documentation, and UTF-8 encoded JSON document is awaited:

                    [static]QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)

                    Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.

                    But I do not understand, do it work for you or do you still have trouble?

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

                    S 1 Reply Last reply 10 Dec 2020, 08:39
                    1
                    • K KroMignon
                      10 Dec 2020, 08:34

                      @SPlatten said in Initialising QJsonObject with JSON:

                      QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());

                      I would change this to QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8());
                      Because, as written in documentation, and UTF-8 encoded JSON document is awaited:

                      [static]QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)

                      Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.

                      But I do not understand, do it work for you or do you still have trouble?

                      S Offline
                      S Offline
                      SPlatten
                      wrote on 10 Dec 2020, 08:39 last edited by
                      #9

                      @KroMignon , I need to produce a QJsonObject from the QJsonDocument, so far this isn't working, the JSON is valid, but:

                      QJsonObject objJSON = objJSON.object();
                      

                      Returns and empty object.

                      Kind Regards,
                      Sy

                      J 1 Reply Last reply 10 Dec 2020, 08:49
                      0
                      • S SPlatten
                        10 Dec 2020, 08:39

                        @KroMignon , I need to produce a QJsonObject from the QJsonDocument, so far this isn't working, the JSON is valid, but:

                        QJsonObject objJSON = objJSON.object();
                        

                        Returns and empty object.

                        J Offline
                        J Offline
                        JonB
                        wrote on 10 Dec 2020, 08:49 last edited by JonB 12 Oct 2020, 08:52
                        #10

                        @SPlatten

                        QJsonObject objJSON = objJSON.object();

                        How can this possibly be right? You have the same variable name, objJSON, on both sides of the =!!

                        For your literal, where you have embedded \"s everywhere, you should use the R"( C++ literal syntax.

                        S 1 Reply Last reply 10 Dec 2020, 08:51
                        0
                        • J JonB
                          10 Dec 2020, 08:49

                          @SPlatten

                          QJsonObject objJSON = objJSON.object();

                          How can this possibly be right? You have the same variable name, objJSON, on both sides of the =!!

                          For your literal, where you have embedded \"s everywhere, you should use the R"( C++ literal syntax.

                          S Offline
                          S Offline
                          SPlatten
                          wrote on 10 Dec 2020, 08:51 last edited by SPlatten 12 Oct 2020, 08:52
                          #11

                          @JonB , sorry that was a typo, the actual code is:

                          QJsonObject objJSON = docJSON.object();
                          

                          As I said, objJSON contains (0 items).

                          Kind Regards,
                          Sy

                          J 1 Reply Last reply 10 Dec 2020, 08:54
                          0
                          • S SPlatten
                            10 Dec 2020, 08:51

                            @JonB , sorry that was a typo, the actual code is:

                            QJsonObject objJSON = docJSON.object();
                            

                            As I said, objJSON contains (0 items).

                            J Offline
                            J Offline
                            JonB
                            wrote on 10 Dec 2020, 08:54 last edited by
                            #12

                            @SPlatten
                            In the past I told you to check the return result from QJsonDocument::fromJson(), and pass in the optional QJsonParseError *error parameter to it....

                            S 1 Reply Last reply 10 Dec 2020, 08:56
                            2
                            • J JonB
                              10 Dec 2020, 08:54

                              @SPlatten
                              In the past I told you to check the return result from QJsonDocument::fromJson(), and pass in the optional QJsonParseError *error parameter to it....

                              S Offline
                              S Offline
                              SPlatten
                              wrote on 10 Dec 2020, 08:56 last edited by SPlatten 12 Oct 2020, 08:58
                              #13

                              @JonB said in Initialising QJsonObject with JSON:

                              al QJsonParseError *error

                              QJsonParseError error;
                              QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8(), &error);
                              QJsonObject objJSON = docJSON.object();
                              

                              error is 14, offset 1410, 14 =:

                              The parsed document contains additional garbage characters at the end
                              

                              Looks like I don't need the ';' at the end.
                              [edit], yes that was it. thank you.

                              Kind Regards,
                              Sy

                              J 1 Reply Last reply 10 Dec 2020, 08:58
                              0
                              • S SPlatten
                                10 Dec 2020, 08:56

                                @JonB said in Initialising QJsonObject with JSON:

                                al QJsonParseError *error

                                QJsonParseError error;
                                QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8(), &error);
                                QJsonObject objJSON = docJSON.object();
                                

                                error is 14, offset 1410, 14 =:

                                The parsed document contains additional garbage characters at the end
                                

                                Looks like I don't need the ';' at the end.
                                [edit], yes that was it. thank you.

                                J Offline
                                J Offline
                                JonB
                                wrote on 10 Dec 2020, 08:58 last edited by JonB 12 Oct 2020, 09:01
                                #14

                                @SPlatten
                                Please, I did suggest to you, start by using the R"( C++ literal syntax. I don't know where in your string there is an error, you'll have to check the offset....

                                Looks like I don't need the ';' at the end.

                                Yep, the JSON does not want/parse any ;....

                                Now you will test the returned QJsonDocument::fromJson() for isNull(), and pass in the error return variable from all of your calls from now onward won't you? :)

                                1 Reply Last reply
                                4

                                1/14

                                10 Dec 2020, 07:50

                                • Login

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