C++ nlohmann JSON 获取数组名称
C++ nlohmann JSON get name of array
I have nlohmann json object:
json uuid = R"(
{
"uuid": ["aaa","bbb","ccc"]
}
)"_json;
我可以毫无问题地获取数组中的值:str = uuid["uuid"][0];
但是我怎样才能得到数组名称呢?
您可以从 json 对象获取底层映射,该对象为您提供数组名称和数组。如果您只想循环访问这些项目,这也很容易。
#include <iostream>
#include <json.hpp>
using json = nlohmann::json;
int main()
{
json uuid = R"(
{
"uuid": ["aaa","bbb","ccc"],
"uuie": ["aaa","bbb","ccc"],
"uuif": ["aaa","bbb","ccc"]
}
)"_json;
if (uuid.is_object())
{
auto obj = uuid.get<json::object_t>();
for (auto& kvp : obj)
{
std::cout << kvp.first << ":" << kvp.second << "n";
}
}
for (auto& item : uuid)
{
std::cout << item << "n";
}
return 0;
}
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 使用指针从C++中的数组中获取最大值
- 获取字符串的长度并将其分配给数组
- 数组长度,为什么从命令行获取时不能使用它?
- 如何在 nlohmann 的 json 库中获取数组长度?
- 在c++中获取两个大int,并将它们存储在数组中
- 如何在C++中获取二维数组中最少的一列数?
- 从 numpy 数组获取指针以将图像发送到C++
- 指针数组.获取指针元素的值
- 循环以使用 cstring 数组 c++ 获取用户输入
- 一种从元组/数组获取参数包的方法
- 如何使用数组获取输入,并将其存储在record.h类的主类中的变量中
- 如何使用数组获取命令行参数
- 从 JSON 数组获取天气
- 布尔数组获取意外的默认值
- 填充列表<>使用数组获取错误的数字
- 如何在C++中为矢量数据结构中的二维数组获取用户输入,而不使用行和列上的两个for循环
- 在自定义库中操作2d C++数组-获取行数
- 使用数组获取测试结果
- 数组c++获取值