HxUtils/HxJson.cpp
2024-01-25 18:37:25 +08:00

58 lines
1.2 KiB
C++

#include "HxJson.h"
#include <QVariant>
int HxJson::to_int(QJsonObject object, QString key)
{
auto value = object.value(key);
switch(value.type())
{
case QJsonValue::Double:
return qRound(value.toDouble());
case QJsonValue::String:
return value.toString().toInt();
default:
return 0;
}
}
bool HxJson::to_boolean(QJsonObject object, QString key)
{
auto value = object.value(key);
switch(value.type())
{
case QJsonValue::Bool:
return value.toBool();
case QJsonValue::String:
return QVariant(value.toString()).toBool();
default:
return false;
}
}
QString HxJson::to_string(QJsonObject object, QString key)
{
auto value = object.value(key);
switch(value.type())
{
case QJsonValue::Bool:
case QJsonValue::Double:
return QString::number(value.toDouble());
case QJsonValue::String:
return value.toString();
default:
return "";
}
}
QStringList HxJson::to_string_list(QJsonObject object, QString key, QString split){ return to_string(object, key).split(split);}