Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能不能用这个开源项目把rpg的值,翻译成中文 #38

Open
xingwozhonghua126 opened this issue Jun 13, 2023 · 3 comments
Open

Comments

@xingwozhonghua126
Copy link

https://github.com/meodai/color-names

@xingwozhonghua126
Copy link
Author

在Qt C++中,您可以使用QJsonDocument类和QJsonObject类读取JSON文件,并将其中的颜色名称和RGB值映射到一起。以下是一个示例程序,它会从JSON文件中读取颜色名称和RGB值,并将它们打印出来:

#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QMap>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // Read the JSON file
    QFile jsonFile("colors.json");
    if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "Failed to open the JSON file";
        return -1;
    }
    QByteArray jsonData = jsonFile.readAll();
    jsonFile.close();

    // Parse the JSON data
    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(jsonData, &error);
    if (doc.isNull()) {
        qDebug() << "Failed to parse the JSON data:" << error.errorString();
        return -1;
    }

    // Get the color map from the JSON object
    QJsonObject colorObject = doc.object();
    QMap<QString, QString> colorMap;
    for (auto itr = colorObject.begin(); itr != colorObject.end(); ++itr) {
        QString rgbValue = itr.key();
        QString colorName = itr.value().toString();
        colorMap.insert(rgbValue, colorName);
    }

    // Print the color map
    for (auto itr = colorMap.begin(); itr != colorMap.end(); ++itr) {
        QString rgbValue = itr.key();
        QString colorName = itr.value();
        qDebug() << "RGB value:" << rgbValue << "Color name:" << colorName;
    }

    return a.exec();
}

在上面的代码中,我们首先使用QFile类打开JSON文件,并将其读取到一个字节数组中。然后,我们使用QJsonDocument类解析JSON数据,并将其转换为QJsonObject对象。

接下来,我们遍历QJsonObject对象,将其中的颜色名称和RGB值映射到QMap对象中。最后,我们使用QMap对象打印RGB值和对应的颜色名称。

请注意,在实际应用程序中,您可能需要在读取JSON文件之前检查文件是否存在,并动态地构建QMap对象。此外,如果您的JSON文件格式与示例不同,您可能需要调整代码以适应不同的数据结构。

@xingwozhonghua126
Copy link
Author

也可以用在线的

@xingwozhonghua126
Copy link
Author

我看一个类似的开源项目https://gitee.com/caxilimao/TSFileEditor,也许有参考的价值

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants