【问题标题】:Fetching api with nlohmann/json使用 nlohmann/json 获取 api
【发布时间】:2021-01-29 01:12:03
【问题描述】:

我想将 api 与 c++ 一起使用,当我搜索时,我发现 nlohmann/json 库看起来很受欢迎,但没有人谈论如何获取 fetch 函数提供的数组。如何从 api 获取信息作为我的 cpp 文件中的变量

【问题讨论】:

    标签: c++ json api c++11 nlohmann-json


    【解决方案1】:

    不太明白您的描述,我假设您的意思是要获取 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多