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. From JSON string to JSON object

From JSON string to JSON object

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 2.9k 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 12 Apr 2020, 09:30 last edited by SPlatten 4 Dec 2020, 09:31
    #1

    I want to convert JSON strings into JSON objects which I can use in C++. I know of QJsonValue and QJsonObject, I'm probably just using them wrong.

    As an example I am passing a const char* which points to:

        [{"failure":"onFileError",
             "file":["~/XMLMPAM/config/simon.txt", "wt"],
               "op":"open",
          "success":"onFileWrite"},
          {"binary":[10,11,12 ],
               "op":"write",
             "text":"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"},
            {"name":"simon",
               "op":"save"},
          {"binary":[10,11,12 ],
               "op":"write",
             "text":"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"},
            {"name":"simon",
               "op":"restore"},
          {"binary":[25,27,28 ],
               "op":"write",
             "text":"HELLO"},
              {"op":"close"}]
    

    The above is an array of objects. I start of by creating an instance of QJsonValue:

        QJsonValue valJSON(cpszJSON);
    

    I then test for it being an array or an object:

        if ( valJSON.isArray() == true || valJSON.isObject() == true ) {
            objJSON = valJSON.toObject();
        }
    

    It doesn't pass either of these tests, I've validated that the JSON string I'm passing is of the correct syntax, why does it fail and is there a way to do this?

    I used http://json.parser.online.fr to verify the syntax.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 12 Apr 2020, 09:52 last edited by
      #2

      See ctor of QJsonValue(const char*) - it creates a new JSon value with the given string. There is nothing about parsing.
      You're looking for QJsonDocument::fromJson()

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

      1 Reply Last reply
      3

      1/2

      12 Apr 2020, 09:30

      • Login

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