【发布时间】:2020-09-08 07:08:06
【问题描述】:
所以我尝试使用https://github.com/nlohmann/json 读取 JSON 文件中的每个字符串,并将字符串推送到地图中。我需要它来读取语言文件中的每个字符串以进行序列化。示例:
{
"StringToRead1" : "Test",
"StringToRead2" : "Another Test"
}
所以我尝试使用迭代器并将所有内容推入:
std::ifstream iStream(filePath);
if(!iStream.is_open()) { std::cout << "Cannot open the strings language file.\n"; return -1; }
nlohmann::json json = nlohmann::json::parse(iStream);
for(auto a = json.begin(); a != json.end(); ++a) {
std::map<std::string, std::string>::iterator iterator = m_Strings.begin();
m_Strings.insert(iterator, std::pair<std::string, std::string>(a.key, a.value));
}
我收到以下编译错误: 错误 C3867:'nlohmann::detail::iter_impl>>>::key':语法不标准;使用'&'创建一个指针 错误 C3867:'nlohmann::detail::iter_impl>>>::value':语法不标准;使用'&'创建指针
感谢您的帮助,我希望我已经足够清楚了。
解决方案: a.key() 和 a.value() 而不是 a.key 和 a.value 谢谢你
【问题讨论】:
-
你得到了什么?
-
更新了帖子,即使我在 a.key/a.value 中添加 & 仍然给我一个语法错误
-
在您链接到的页面上的 README 中有一个示例... (ctrl+f "special iterator member functions for objects")
-
这也是写给
std::map的一种非常冗长的方式...为什么不直接写m_Strings[a.key()] = a.value();呢? -
或
m_Strings.emplace(a.key(), a.value());