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. converting JSON object to class

converting JSON object to class

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.5k Views 2 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.
  • D Offline
    D Offline
    Dimple
    wrote on last edited by Dimple
    #1

    hi ,,, can anybody pls suggest me to convert from QJsonObject to class

    here i created one object (room=new XperienceRoom();) in DataManager class to use xperience room class ,,
    void DataManager::setRoom(QJsonObject response)
    {
    // room = response;
    room=response["room"].toObject();
    }

    QJsonObject DataManager::getRoom()
    {
    return room;
    }

    here i'am getting error "Assigning to' XperienceRoom *' from inocmpatible type 'QJsonObject'

    raven-worxR JKSHJ 2 Replies Last reply
    0
    • D Dimple

      hi ,,, can anybody pls suggest me to convert from QJsonObject to class

      here i created one object (room=new XperienceRoom();) in DataManager class to use xperience room class ,,
      void DataManager::setRoom(QJsonObject response)
      {
      // room = response;
      room=response["room"].toObject();
      }

      QJsonObject DataManager::getRoom()
      {
      return room;
      }

      here i'am getting error "Assigning to' XperienceRoom *' from inocmpatible type 'QJsonObject'

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Dimple
      thats not possible directly (the way you tried).
      Does the JSON object have the same properties like your class?
      If so you will need to copy each property "manually"

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • D Offline
        D Offline
        Dimple
        wrote on last edited by
        #3

        yes JSON object has the same properties like class

        1 Reply Last reply
        0
        • D Dimple

          hi ,,, can anybody pls suggest me to convert from QJsonObject to class

          here i created one object (room=new XperienceRoom();) in DataManager class to use xperience room class ,,
          void DataManager::setRoom(QJsonObject response)
          {
          // room = response;
          room=response["room"].toObject();
          }

          QJsonObject DataManager::getRoom()
          {
          return room;
          }

          here i'am getting error "Assigning to' XperienceRoom *' from inocmpatible type 'QJsonObject'

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

          @Dimple said in converting JSON object to class:

          hi ,,, can anybody pls suggest me to convert from QJsonObject to class

          Here is an example: http://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

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

          1 Reply Last reply
          3
          • D Offline
            D Offline
            Dimple
            wrote on last edited by
            #5

            i got solution ,,,its a correct way
            void DataManager::setRoom(QJsonObject json)
            {
            QString id =json["_id"].toString();
            QString contactid = json["contact_id"].toString();
            QString createdate = json["create_date"].toString();
            QString createrid = json["creater_id"].toString();
            bool deleted = json["deleted"].toBool();
            bool finished = json["finished"].toBool();
            int historyindex = json["history_index"].toInt();
            bool isgroup = json["is_group"].toBool();

            room->setId(id);
            room->setContactId(contactid);
            room->setCreateDate(createdate);
            room->setCreaterId(createrid);
            room->setDeleted(deleted);
            room->setFinished(finished);
            room->setHistoryIndex(historyindex);
            room->setIsGroup(isgroup);

            }

            XperienceRoom* DataManager::getRoom()
            {
            return room;
            }

            raven-worxR 1 Reply Last reply
            0
            • D Dimple

              i got solution ,,,its a correct way
              void DataManager::setRoom(QJsonObject json)
              {
              QString id =json["_id"].toString();
              QString contactid = json["contact_id"].toString();
              QString createdate = json["create_date"].toString();
              QString createrid = json["creater_id"].toString();
              bool deleted = json["deleted"].toBool();
              bool finished = json["finished"].toBool();
              int historyindex = json["history_index"].toInt();
              bool isgroup = json["is_group"].toBool();

              room->setId(id);
              room->setContactId(contactid);
              room->setCreateDate(createdate);
              room->setCreaterId(createrid);
              room->setDeleted(deleted);
              room->setFinished(finished);
              room->setHistoryIndex(historyindex);
              room->setIsGroup(isgroup);

              }

              XperienceRoom* DataManager::getRoom()
              {
              return room;
              }

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @Dimple said in converting JSON object to class:

              i got solution ,,,its a correct way

              basically yes.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1

              • Login

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