Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

c++ - QString replace characters outside of quotes

I'm currently reading in a json file to a QJsonDocument and then converting it to a QString such that it looks like:

"thing1":"thing1value",

"thing2":"thing2,value",

"thing3":"thing3value",

Each line ends with a comma and I would like to try and format it a bit to show to the user without the quotations and a new line after each line. I was attempting use the replace function to replace any commas with a new line. The problem is that just using the replace function to replace all commas with also means that anything inside the quotes is altered too like:

"thing2":"thing2

value"

Is there any easy way to alter the values exterior of quotes without touching the insides? Is there perhaps a better approach to the overall formatting I could be doing instead? I have a feeling this is asking a bit much of Qt and I may just have to write some function that indexOf finds the first quote and tracks it to the next etc.

Here's what I have so far essentially

    QString jDoc = file.readAll();
    file.close();

    QJsonParseError parserError;
    QJsonDocument jsonInit = QJsonDocument::fromJson(jDoc.toUtf8(), &parserError);
    qDebug() << parserError.errorString();
    QString strJson(jsonInit.toJson(QJsonDocument::Indented));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution

Use QString::right to retrieve the value and then QString::arg to format the output to your liking.

Example

Here is a minimal example I have prepared for you of how to implement this solution:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QJsonObject json {
        {"thing1", "thing1value1"},
        {"thing2", "thing2,value2"},
        {"thing3", "thing3value3"}
    };

    QString newText;

    foreach (const QString &key, json.keys()) {
        const QJsonValue &value(json.value(key));
        const QString &str(value.toString().mid(key.length()));

        newText.append(QString(""%1":"%1
\%2"").arg(key).arg(str.startsWith(",") ? str.mid(1) : str));

        if (key != json.keys().last())
            newText.append("

");
    }

    auto *textBrowser = new QTextBrowser(this);

    textBrowser->setText(newText);

    setCentralWidget(textBrowser);
}

Result

The example produces the following result:

Window with the processed JSON

Credit

Special thanks for the valuable help of correcting the bug in the code to Some Programmer Dude (@Someprogrammerdude):

https://stackoverflow.com/a/52402760/5366641


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...