读取JSON中的每个字符串

问题描述 投票:0回答:1

因此,我尝试使用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谢谢

c++ json file iostream
1个回答
1
投票

键和值是函数调用,因此您需要使用函数运算符:

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()));
}

已插入

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));
}
© www.soinside.com 2019 - 2024. All rights reserved.