我用C ++编写了一个程序,现在,我可以读取JSON文件,并且能够执行指令。这里我附加了Sample JSON文件(预定义),按照Json指令程序执行,当它到达结束坐标时程序将停止。
用于启动和停止通过Web套接字从服务器接收的字符串。如果从服务器单击开始按钮,应用程序将启动,在Json文件中我已经定义了映射坐标start_x,start_y将到达end_x,end_y坐标应用程序将停止。
现在,我想从服务器发送start_x,start_y,end_x,end_y坐标。每当坐标值来自服务器时,应该更新(更改/写入)JSON中的坐标值,而不仅仅是值,无论从服务器发送的字符串是什么。我知道如何读取JSON并执行指令,我不知道如何从服务器编写,替换,更新JSON。然后我想执行说明。
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 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();
QJsonValue endy = item["end_y"];
endvaluey = endy.toInt();
yendvaluestring = endy.toString();
请帮助我解决这个问题。