简体   繁体   中英

How to parse data in Qt C++ without use of json?

I am using Qt Creator 3.5.1 (opensource) Based on Qt 5.5.1 (GCC 4.9.1, 32 bit) on Ubuntu 14.04 and developing app for embedded linux device. In my app I get some currency in every 30 second. So, In my main window I set the QThread and QTimer and using QNetworkAccessManager and QNetworkRequest I get the below data. Now I have a 6 label on my main window such as;

lblBuy_USD, lblBuy_EUR, lblBuy_STG, lblSale_USD, lblSale_EUR, lblSale_STG

My problem is I cannot use json file in my Qt. So, my question is that how can extract dollar-sale-data (which is 3,9500) from the data that I get from QNetworkRequest?

{
    "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"
        }

      }
}

UPDATE: I use regular expressions but I couldn't get any data. My label has no value. Any help please?

       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 ));
         }

In order to get Dollar sales you can try to do the following:

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
}

UPDATE

The same can be achieved by using QRegExp classes (old):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM