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 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

      • Login

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