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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #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

    KroMignonK JonBJ 2 Replies Last reply
    0
    • SPlattenS SPlatten

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

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #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)

      SPlattenS 1 Reply Last reply
      1
      • SPlattenS SPlatten

        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"};
        
        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #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)

        SPlattenS 1 Reply Last reply
        1
        • KroMignonK KroMignon

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

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on 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

          JonBJ KroMignonK 2 Replies Last reply
          0
          • SPlattenS SPlatten

            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"};
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            • SPlattenS SPlatten

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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
              • SPlattenS SPlatten

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

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #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)

                SPlattenS 1 Reply Last reply
                1
                • KroMignonK KroMignon

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

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by SPlatten
                  #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

                  KroMignonK 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

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

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by KroMignon
                    #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)

                    SPlattenS 1 Reply Last reply
                    1
                    • KroMignonK KroMignon

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

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on 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

                      JonBJ 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #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.

                        SPlattenS 1 Reply Last reply
                        0
                        • JonBJ JonB

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

                          SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by SPlatten
                          #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

                          JonBJ 1 Reply Last reply
                          0
                          • SPlattenS SPlatten

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

                            QJsonObject objJSON = docJSON.object();
                            

                            As I said, objJSON contains (0 items).

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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....

                            SPlattenS 1 Reply Last reply
                            2
                            • JonBJ JonB

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

                              SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by SPlatten
                              #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

                              JonBJ 1 Reply Last reply
                              0
                              • SPlattenS SPlatten

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

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #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

                                • Login

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