c++ 使用Qt将JavaScript转储数组解析为点列表

n6lpvg4x  于 11个月前  发布在  Java
关注(0)|答案(2)|浏览(89)

我需要使用c++将一个转储的点数组转换为qt中的点列表,这样我就可以进一步调试我的应用程序。我使用Chrome将数组对象复制到一个文件中,它将像这样结束:

[
  {
    "x": 0,
    "y": 266.48351648351644
  },
  {
    "x": 2.3095238095238093,
    "y": 274.3003663003663
  },
  {
    "x": 3.3754578754578755,
    "y": 277.6758241758242
  }
]

字符串
我的策略是打开文件,找到其中包含字符x或y的行,并将每个连续的x,y保存保存在一个point对象中:

void MainWindow::on_btnOpenFile_clicked()
{

    auto path = QFileDialog::getOpenFileName(this, "Browse for a array object file...");
    QFile file(path); // works fine

    if(file.exists(path) && file.open(QIODevice::ReadOnly)) {

        QTextStream stream(&file);
        QVector<QPointF> points;
        QString currentLine;
        qreal x;
        qreal y;

        // Regex rules for x and y
        QRegExp rX(":.*,");
        rX.setMinimal(true);
        QRegExp rY(":.*");
        rY.setMinimal(true);

        while(!stream.atEnd()) {

            bool xFound = false, yFound = false;
            currentLine = stream.readLine();

            // Get X value
            if(currentLine.contains("x", Qt::CaseInsensitive)) {
                auto pos = rX.indexIn(currentLine);
                if(pos == 0) {
                    x = rX.capturedTexts()[0].toDouble();
                    xFound = true;
                }
            } else if(currentLine.contains("y", Qt::CaseInsensitive)) { // Get Y value
                auto pos = rY.indexIn(currentLine);
                if(pos == 0) {
                    y = rY.capturedTexts()[0].toDouble();
                    yFound = true;
                }
            }

            if(xFound)
            points.push_back({x,y});

        }
    }

}


我只是注意到我的方法不起作用,因为我在while循环的每次迭代中只计算一行.
第二个是我的正则表达式,它甚至匹配[字符!
有没有更聪明的方法来进行这种解析?转储的文件对我来说看起来像JSON,但也许不是?如果它与JSON兼容,有没有自动转换的方法?

ma8fv8wu

ma8fv8wu1#

你正在尝试重新发明轮子。不要尝试使用正则表达式。将文件读入JSON文档。类似这样的东西应该可以工作(未经测试):

QFile file("myfile.json");
    QVector<QPointF> points;

    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
        file.close();

        QJsonArray array = doc.array();

        for (auto &&point : array)
        {
            QJsonObject p = point.toObject();
            points.push_back({p["x"], p["y"]});
        }
    }

字符串

thtygnil

thtygnil2#

当然,最好的方法是将其作为JSON文件读取和解析.但是如果你想走老路,使用循环来做,没有人说你每次迭代只能读取一行.你可以像这样实现你想要的:

void MainWindow::on_btnOpenFile_clicked()
{

    auto path = QFileDialog::getOpenFileName(this, "Browse for a array object file...");
    QFile file(path); // works fine

    if(file.exists(path) && file.open(QIODevice::ReadOnly)) {

        QTextStream stream(&file);
        QString xLine;
        QString yLine;
        QVector<QPointF> mPoints;

        while(!stream.atEnd()) {

            xLine = stream.readLine();

            if(xLine.contains("x", Qt::CaseInsensitive)) { // Line has x in it
                yLine = stream.readLine(); // READ NEXT LINE!!!!
            } else { // Line is either Y or not a X line so we skip to next iteration
                continue;
            }

            // Debug the lines
            qDebug() << "X =" << xLine;
            qDebug() << "Y =" << yLine;

            // Parse and add the points to the array    
            auto xConvResult = false;
            auto yConvResult = false;

            // To discard trailing ',' of the X line
            auto xValue = xLine.split(":")[1].split(",")[0].toDouble(&xConvResult);
            auto yValue = yLine.split(":")[1].toDouble(&yConvResult);

            if(xConvResult  && yConvResult) {
               mPoints.push_back({xValue, yValue});
            }
        }

        file.close();
    }
}

字符串

相关问题