gpt4 book ai didi

c++ - 如何在不使用 json 的情况下解析 Qt C++ 中的数据?

转载 作者:行者123 更新时间:2023-11-28 04:55:39 25 4
gpt4 key购买 nike

我在 Ubuntu 14.04 上使用基于 Qt 5.5.1(GCC 4.9.1,32 位)的 Qt Creator 3.5.1(开源)并为嵌入式 Linux 设备开发应用程序。在我的应用程序中,我每 30 秒获得一些货币。因此,在我的主窗口中,我设置了 QThread 和 QTimer,并使用 QNetworkAccessManager 和 QNetworkRequest 我得到了以下数据。现在我的主窗口上有一个 6 标签,例如;

lblBuy_USD、lblBuy_EUR、lblBuy_STG、lblSale_USD、lblSale_EUR、lblSale_STG

我的问题是我不能在我的 Qt 中使用 json 文件。所以,我的问题是如何从我从 QNetworkRequest 获得的数据中提取美元销售数据(即 3,9500)?

{
"date": "20171108",
"currencies": {

"dollar": {
"buy": "3,8450",
"sale": "3,9500",
"e_buy": "3,8450"
},

"sterling": {
" buy ": "5,0500",
" sale ": "5,1700",
" e_buy ": "5,0500"
},

"euro": {
" buy ": "4,4600",
" sale ": "4,5650",
" e_buy ": "4,4600"
}

}
}

更新:我使用正则表达式,但无法获取任何数据。我的标签没有值(value)。有什么帮助吗?

       QString strReply = (QString)currentReply->readAll();

QRegExp rxBUY_USD("dollar.*?buy.*?(\\d+\\,\\d+)");
if( rxBUY_USD.indexIn( strReply ) != -1 )
{
ui->lblBUY_USD->setText( rxBUY_USD.cap( 1 ));
}

最佳答案

为了获得美元销售额,您可以尝试执行以下操作:

QRegularExpression re("dollar.*?sale.*?(\\d+\\,\\d+)"); // Watch the decimal separator
QRegularExpressionMatch match = re.match(s); // s - is the JSON string you got
if (match.hasMatch())
{
QString matched = match.captured(1);
// Convert string to number, if needed.
}
else
{
// Failed to find dollar sales
}

更新

同样可以通过使用QRegExp类(旧)来实现:

QRegExp re2("dollar.*sale.*(\\d+\\,\\d+).*");
if (re2.indexIn(s) != -1)
{
QString matched = re2.cap(1);
}

关于c++ - 如何在不使用 json 的情况下解析 Qt C++ 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47180272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com