计算 JSON 中的条目数并相应地执行代码

  • 本文关键字:执行 代码 JSON 计算 c++ qt qt5
  • 更新时间 :
  • 英文 :


我有一个包含航点的 Json 文件。该文件可以包含更多或更少的录音。

{
  "waypoint0": "10.05456, 51.02453, 0.0",
  "waypoint1": "10.05456, 51.02453, 0.0",
  "waypoint2": "10.05456, 51.02453, 0.0",
  "waypoint3": "10.05456, 51.02453, 0.0",
  "waypoint4": "10.05456, 51.02453, 0.0",
  "waypoint5": "10.05456, 51.02453, 0.0",
  "waypoint6": "10.05456, 51.02453, 0.0",
}

首先,我想读取 JSON 文件:标准::矢量路线;

QString filename = QFileDialog::getOpenFileName(this,
                                                tr("Flugweg laden"), "",
                                                tr("Wegpunkt Datei (*.wpf)"));
QString val;
QFile file(filename);
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject sett2 = d.object();
QJsonObject it = d.object();
for(auto it = sett2.begin(); it != sett2.end(); ++it) { 
    //TODO
}

现在,我想为每个对象执行代码,并在写入//TODO的位置进行更改。

est::aladin::Waypoint waypoint0; // waypoint0 should be changed on the name of the values like in my JSON File. waypoint0 to waypoint6
waypoint0.id = 0; // waypoint0 меняется в соответствии кол-во запись на waypoint1 и далее.
waypoint0.displayed_number = -1; // the first waypoint should be have the number -1 and the last -2, waypoints beetween from 1 and till 98.
waypoint0.latitude_wgs84_radians = lat; //first value from waypoint0 - in this example - 10.05456 and etc.
waypoint0.longitude_wgs84_radians = long; //second value from waypoint0 - in this example - 51.02453 and etc.
waypoint0.altitude_wgs84_meters = alt; //third value from waypoint0 - in this example - 0.0 and etc.
waypoint0.type = est::aladin::WaypointType::GroundControlStation; // if it is waypoint0
//between the first and last waypoint WAYPOINT+NUMBER.type = est::aladin::WaypointType::FlyOver;
//Last waypoint WAYPOINT+NUMBER.type = est::aladin::WaypointType::Landing;
route.push_back(waypoint0); // waypoint0 should be changed on which waypoint we will push now.

如何解析 JSON 行"waypoint0": "10.05456, 51.02453, 0.0",并输出到不同的变量。

你的.json是不够的,我认为复制和粘贴它是一个错误(最后一行有一个不必要的逗号(。

我们要做的第一件事是获取QJsonObject,然后遍历其keys(),结果是csv格式的字符串,因此我们必须用split将其分开,然后将每个项目转换为带有toDouble()的双精度。

QString filename = QFileDialog::getOpenFileName(this,
                                            tr("Flugweg laden"), "",
                                            tr("Wegpunkt Datei (*.wpf)"));
    if(!filename.isEmpty()){
        QByteArray val;
        QFile file(filename);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        val = file.readAll();
        file.close();
        QJsonDocument d = QJsonDocument::fromJson(val);
        QJsonObject obj = d.object();
        for(const QString &key: obj.keys() ){
            QString line = obj[key].toString();
            QStringList elements = line.split(",");
            double lat= elements[0].toDouble();
            double lng= elements[1].toDouble();
            double alt= elements[2].toDouble();
            qDebug()<<key<< lat<<lng<<alt;
            est::aladin::Waypoint waypoint;
            [...]
            waypoint.latitude_wgs84_radians = lat;
            waypoint.longitude_wgs84_radians = lng;
            waypoint.altitude_wgs84_meters = alt; 
            [...]
            route.push_back(waypoint);   
        }
    }

注意:不要使用 long 作为变量的名称,因为它是保留名称。

相关内容

  • 没有找到相关文章

最新更新