【发布时间】:2021-01-29 01:12:03
【问题描述】:
我想将 api 与 c++ 一起使用,当我搜索时,我发现 nlohmann/json 库看起来很受欢迎,但没有人谈论如何获取 fetch 函数提供的数组。如何从 api 获取信息作为我的 cpp 文件中的变量
【问题讨论】:
标签: c++ json api c++11 nlohmann-json
我想将 api 与 c++ 一起使用,当我搜索时,我发现 nlohmann/json 库看起来很受欢迎,但没有人谈论如何获取 fetch 函数提供的数组。如何从 api 获取信息作为我的 cpp 文件中的变量
【问题讨论】:
标签: c++ json api c++11 nlohmann-json
不太明白您的描述,我假设您的意思是要获取 JSON 数组?你可以试试这个:
std::string ss= R"(
{
"test-data":
[
{
"name": "tom",
"age": 11
},
{
"name": "jane",
"age": 12
}
]
}
)";
json myjson = json::parse(ss);
auto &students = myjson["test-data"];
for(auto &student : students) {
cout << "name=" << student["name"].get<std::string>() << endl;
}
【讨论】: