[英]How can I get an array of all currency symbols and currency abbreviations in C++ or Qt?
I need to get all possible currency symbols and currency abbreviations in my program in an array.我需要在我的程序中以数组的形式获取所有可能的货币符号和货币缩写。 How can I achieve this with C++ or possibly with Qt?
我怎样才能用 C++ 或 Qt 实现这一点?
I found the currency codes JSON list https://gist.github.com/Fluidbyte/2973986 .我找到了货币代码 JSON 列表https://gist.github.com/Fluidbyte/2973986 。
This list is regularly updated.此列表会定期更新。
How the list could be parsed.如何解析列表。 Download the raw JSON text from the link.
从链接下载原始 JSON 文本。 Save it to an UTF-8 encoded text file.
将其保存为 UTF-8 编码的文本文件。 Parse it somehow about written below.
以某种方式解析它写在下面。
#include <QtWidgets/QApplication>
#include <QVariantMap>
#include <QStringList>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
#include <QPlainTextEdit>
using CurrencyMap = QMap<QString, QVariantMap>; // Use three-letter code as a key
using CurrencyMapIterator = QMapIterator<QString, QVariantMap>;
CurrencyMap parseCurrencyDataFromJson(QByteArray utf8Json)
{
CurrencyMap ret;
QJsonParseError parseError;
QJsonDocument document = QJsonDocument::fromJson(utf8Json, &parseError);
if (parseError.error != QJsonParseError::NoError)
{
qWarning() << parseError.errorString();
}
if (!document.isNull())
{
QJsonObject jobject = document.object();
// Iterate over all the currencies
for (QJsonValue val : jobject)
{
// Object of a given currency
QJsonObject obj = val.toObject();
// Three-letter code of the currency
QString name = obj.value("code").toString();
// All the data available for the given currency
QVariantMap fields = obj.toVariantMap();
ret.insert(name, fields);
}
}
return ret;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPlainTextEdit plainTextEdit;
plainTextEdit.show();
// File saved from the GitHub repo
QFile file("curr.json");
if (file.open(QIODevice::ReadOnly))
{
QByteArray jsonData = file.readAll();
CurrencyMap currencyMap = parseCurrencyDataFromJson(jsonData);
// Output all the available currency symbols to the plainTextEdit
CurrencyMapIterator currency(currencyMap);
while (currency.hasNext())
{
currency.next();
QString code = currency.key();
QVariantMap fileds = currency.value();
QStringList currencyInfo
{
code,
fileds.value("symbol").toString(),
fileds.value("symbol_native").toString()
};
plainTextEdit.appendPlainText(currencyInfo.join('\t'));
}
QString total = QString("\nTotal Available Currencies Count = %1")
.arg(currencyMap.count());
plainTextEdit.appendPlainText(total);
}
return a.exec();
}
QChar
and Unicode based solution.基于
QChar
和 Unicode 的解决方案。 But be note that QChar
itself supports codes up to 0xFFFF maximum only.但请注意,
QChar
本身仅支持最大 0xFFFF 的代码。 Therefore only limited amount of symbols can be retrieved this way.因此,只能以这种方式检索有限数量的符号。
// In Qt, Unicode characters are 16-bit entities.
// QChar itself supports only codes with 0xFFFF maximum
QList<QChar> getAllUnicodeSymbolsForCategory(QChar::Category cat)
{
QList<QChar> ret;
// QChar actually stores 16-bit values
const static quint16 QCharMaximum = 0xFFFF;
for (quint16 val = 0; val < QCharMaximum; val++)
{
QChar ch(val);
if (ch.category() == cat)
{
ret.append(ch);
}
}
return ret;
}
Usage用法
QList<QChar> currencies = getAllUnicodeSymbolsForCategory(QChar::Symbol_Currency);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.