【发布时间】:2022-01-15 22:46:12
【问题描述】:
我在这个 json 中有这个配置数据,
{
"difficulty":-1,
"damage":100,
"infinite":true,
"tilewidth":16,
"type":"map",
"version":"1.2.4"
}
我想将这些变量存储在我的程序中。我不能使用 Map,因为没有固定类型可供读取 (int,string,bool)...
using JSON = nlohmann::json;
/* struct Configs final {
int difficulty;
int damage;
bool infinite;
int tilewidth;
std::string type;
std::string version;
}; */
int main(void) {
JSON j;
std::ifstream in("./assets/Map.json");
in >> j;
for (auto &el : j.items()) {
std::cout << el.key() << " : " << el.value() << "\n";
}
return 0;
}
有哪些聪明的方法可以做到这一点?
【问题讨论】:
-
也许是
std::map<std::string, std::any>,如果你知道所有可能的类型std::variant而不是std::any? -
这些字段是否已修复,或者某些字段是否缺失而其他字段是否可以添加等?
标签: c++ json configuration