[{"key":"0","value":"男"},{"key":"1","value":"女"}]转实体map
时间: 2024-01-29 15:10:32 浏览: 42
map和实体之间的转换
以下是将[{"key":"0","value":"男"},{"key":"1","value":"女"}]转换为实体map的示例代码:
```cpp
#include <iostream>
#include <map>
#include <string>
#include <json/json.h>
int main() {
std::string jsonStr = R"([{"key":"0","value":"男"},{"key":"1","value":"女"}])";
Json::Value root;
Json::Reader reader;
if (!reader.parse(jsonStr, root)) {
std::cout << "Failed to parse JSON string." << std::endl;
return 1;
}
std::map<std::string, std::string> entityMap;
for (const auto& item : root) {
std::string key = item["key"].asString();
std::string value = item["value"].asString();
entityMap[key] = value;
}
// 输出实体map
for (const auto& pair : entityMap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
return 0;
}
```
运行以上代码,将会输出以下结果:
```
Key: 0, Value: 男
Key: 1, Value: 女
```
阅读全文