Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt: How to replace number (Start, End) in a JSON from server



  • This is how my JSON file looks like. As you can see, the start_x, start_y, and end_x, end_y, degree_of_rotation is a number.

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

    How can I replace and save the number in JSON from the server? Currently, this is what I have. Qt5 has a new JSON parser and I want to use it. The problem is that it isn't too clear about what the functions do

    void JsonFileread::Json_File_Function()
    {
        try
        {
            qDebug() <<"file reading";
    
            file.setFileName("/home/JSON/jsonfile.js");   
            file.open(QIODevice::ReadOnly | QIODevice::Text);
    
            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 mainid = 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();
            qWarning() << endvaluex<<endl;
    
            QJsonValue endy = item["end_y"]; 
            endvaluey = endy.toInt();
    
            yendvaluestring = endy.toString();
            qWarning() << endvaluey<<endl;
    
            QJsonValue motion = item.value(QString("motion"));
            QJsonObject motion_object = motion.toObject();
    
            QJsonValue motiontype = motion_object["motion_type"];
            motiontypestring = motiontype.toString();
    
            QJsonValue directionlinear = motion_object["linear_direction"];
            lineardirection = directionlinear.toString();
    
            QJsonValue rotatingangle = motion_object["degree_of_rotation"];
            angleofrotation = rotatingangle.toInt();
        }
    
        catch (QJsonParseError error)
            {
                qDebug() <<  error.errorString();
            }
        catch(...)
            {
                qDebug() <<  "due to some other error";
            }
    }
    

    this is only about reading JSON values, I don't know how to change or replace, For example, consider the above code, reading the ''start_x, start_y, and end_x, end_y'' then this value sent to the server. Now, I have received the value from the server, but whenever we received ''start_x, start_y, and end_x, end_y" value, JSON value should change or replace according to that value. Ex: ('Startx 0 replace5 (startx 5), starty 4 to starty 7' ). Receive JSON data from the server to client(Processor), received value will replace to JSON each and every time.


  • Lifetime Qt Champion

    Hi,

    Your question seems similar that thread.

    Hope it helps


Log in to reply