"interface_collaboration": { "type":"index", "interface_collaboration": [ {"1":{"key":"add"}, "2":{"key":"modify"}, "3":{"key":"delete"}, "4":{"key":"show"}} ] }如何遍历
时间: 2023-07-24 17:13:56 浏览: 49
要遍历 `interface_collaboration` 中的键和值,您可以使用迭代器来访问 JSON 对象的成员。以下是一个示例代码,展示如何遍历 `interface_collaboration` 中的键和值:
```cpp
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
std::string jsonString = R"(
{
"interface_collaboration": {
"type":"index",
"interface_collaboration": [
{"1":{"key":"add"}, "2":{"key":"modify"}, "3":{"key":"delete"}, "4":{"key":"show"}}
]
}
}
)";
json data = json::parse(jsonString);
for (const auto& item : data["interface_collaboration"]["interface_collaboration"]) {
for (const auto& [key, value] : item.items()) {
std::cout << "Key: " << key << ", Value: " << value["key"] << std::endl;
}
}
return 0;
}
```
在这个示例中,我们首先将 JSON 字符串解析为 `json` 对象。然后通过迭代器遍历 `data["interface_collaboration"]["interface_collaboration"]` 中的每一个元素。
在内层循环中,我们使用结构化绑定将键和值分别赋值给 `key` 和 `value`。然后我们可以通过 `value["key"]` 访问嵌套的键。
请根据您的具体需求,在适当的地方修改代码以满足您的要求。
希望能解决您的问题。如果还有其他疑问,请随时提问。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)