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. How to write/Read Json file
Forum Updated to NodeBB v4.3 + New Features

How to write/Read Json file

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 7.3k 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.
  • JerwinprabuJ Offline
    JerwinprabuJ Offline
    Jerwinprabu
    wrote on last edited by
    #1

    Hi,

    I have written a program in C++, Now, I can read the JSON file and I can able to execute the instructions. Here I have attached Sample JSON file (Pre defined), As per the Json instruction program will execute, when it will reach the end coordinate the program will stop.

    For starting and stopping string received from the server through web socket. If the start button clicked from server, application will start, In Json file I have defined the mapping coordinate start_x, start_y when it will reach the end_x, end_y coordinate the application will stop.

    Now, I want to sent the start_x, start_y, end_x, end_y coordinate from server. Whenever the coordinate values are coming from server that should update (Change/Write) the coordinate values in JSON, not only values, whatever the string sending from server. I know how to read JSON and execute the instruction, I don't know how to write, replace, update JSON from server. Then I want to execute the instructions.

    Jsonfile.js

    {
    "ID" : "NO1",
    "action" : "start",
     "zone" :
        {
            "start_x" : 0,
            "start_y" : 4,
    
            "end_x" : 10,
            "end_y" : 5,
    
            "motion" :
            {
                "motion_type": "Linear",
                "degree_of_rotation": 30,
                "rotation_direction": "clock",
                "linear_direction": "+ve"
            }
        }
    }
    

    Jsonfileread.cpp

        qDebug() <<"file reading";
        file.setFileName("/PROJECT/JSON/jsonfile.js"); 
        file.open(QIODevice::ReadOnly | QIODevice::Text);
     
       if (!file.exists()){
            cout<<"file found"<<endl;
        }
    
        QFileDevice::FileError err = QFileDevice::NoError;
    
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug() <<"Could not open file : "<<file.fileName() <<"for reading :"<<file.errorString()<<endl;
            errMsg =file.errorString();
            err =file.error();
            cout<<"err : "<<err<<endl;
        }
    
        settings = file.readAll(); 
    
        if (file.error() != QFile::NoError) {
               qDebug() << QString("Failed to read from file %1, error: %2").arg(file.fileName()).arg(file.errorString());
           }
    
        if (settings.isEmpty()) {
                qDebug() << "No data was currently available for reading from file" << file.fileName();
            }
        file.close();
    
        object = EchoClient::jsonstringflag;
    
        QJsonDocument sd = QJsonDocument::fromJson(settings.toUtf8());
        QJsonObject sett2 = sd.object(); 
    
        QJsonValue mainroboid = sett2["ID"];
        mainid = mainid.toString();
    
        QJsonValue action = sett2["action"];
        actionstring = action.toString();
    
        QJsonValue value = sett2.value(QString("zone")); 
        QJsonObject item = value.toObject();
    
        QJsonValue startx = item["start_x"]; 
        startvaluex = startx.toInt();
    
        QJsonValue starty = item["start_y"];
        startvaluey = starty.toInt();
    
        QJsonValue endx = item["end_x"];
        endvaluex = endx.toInt();
        xendvaluestring = endx.toString();
    
        QJsonValue endy = item["end_y"]; 
        endvaluey = endy.toInt();
        yendvaluestring = endy.toString();
    

    Kindly assist me to solve this issue.

    jsulmJ 1 Reply Last reply
    0
    • JerwinprabuJ Jerwinprabu

      Hi,

      I have written a program in C++, Now, I can read the JSON file and I can able to execute the instructions. Here I have attached Sample JSON file (Pre defined), As per the Json instruction program will execute, when it will reach the end coordinate the program will stop.

      For starting and stopping string received from the server through web socket. If the start button clicked from server, application will start, In Json file I have defined the mapping coordinate start_x, start_y when it will reach the end_x, end_y coordinate the application will stop.

      Now, I want to sent the start_x, start_y, end_x, end_y coordinate from server. Whenever the coordinate values are coming from server that should update (Change/Write) the coordinate values in JSON, not only values, whatever the string sending from server. I know how to read JSON and execute the instruction, I don't know how to write, replace, update JSON from server. Then I want to execute the instructions.

      Jsonfile.js

      {
      "ID" : "NO1",
      "action" : "start",
       "zone" :
          {
              "start_x" : 0,
              "start_y" : 4,
      
              "end_x" : 10,
              "end_y" : 5,
      
              "motion" :
              {
                  "motion_type": "Linear",
                  "degree_of_rotation": 30,
                  "rotation_direction": "clock",
                  "linear_direction": "+ve"
              }
          }
      }
      

      Jsonfileread.cpp

          qDebug() <<"file reading";
          file.setFileName("/PROJECT/JSON/jsonfile.js"); 
          file.open(QIODevice::ReadOnly | QIODevice::Text);
       
         if (!file.exists()){
              cout<<"file found"<<endl;
          }
      
          QFileDevice::FileError err = QFileDevice::NoError;
      
          if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
          {
              qDebug() <<"Could not open file : "<<file.fileName() <<"for reading :"<<file.errorString()<<endl;
              errMsg =file.errorString();
              err =file.error();
              cout<<"err : "<<err<<endl;
          }
      
          settings = file.readAll(); 
      
          if (file.error() != QFile::NoError) {
                 qDebug() << QString("Failed to read from file %1, error: %2").arg(file.fileName()).arg(file.errorString());
             }
      
          if (settings.isEmpty()) {
                  qDebug() << "No data was currently available for reading from file" << file.fileName();
              }
          file.close();
      
          object = EchoClient::jsonstringflag;
      
          QJsonDocument sd = QJsonDocument::fromJson(settings.toUtf8());
          QJsonObject sett2 = sd.object(); 
      
          QJsonValue mainroboid = sett2["ID"];
          mainid = mainid.toString();
      
          QJsonValue action = sett2["action"];
          actionstring = action.toString();
      
          QJsonValue value = sett2.value(QString("zone")); 
          QJsonObject item = value.toObject();
      
          QJsonValue startx = item["start_x"]; 
          startvaluex = startx.toInt();
      
          QJsonValue starty = item["start_y"];
          startvaluey = starty.toInt();
      
          QJsonValue endx = item["end_x"];
          endvaluex = endx.toInt();
          xendvaluestring = endx.toString();
      
          QJsonValue endy = item["end_y"]; 
          endvaluey = endy.toInt();
          yendvaluestring = endy.toString();
      

      Kindly assist me to solve this issue.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Jerwinprabu Take a look at http://doc.qt.io/qt-5/qtcore-json-savegame-example.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

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