Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt: How to replace number (Start, End) in a JSON from server
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
2 Posts 2 Posters 728 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.
  • JerwinprabuJ Offline
    JerwinprabuJ Offline
    Jerwinprabu
    wrote on last edited by A Former User
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Your question seems similar that thread.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

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