How to write/Read Json file



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


  • Moderators


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.