在数组 C++ 中读取数组内部的项目

Reading Items inside of an array in an array C++

我有一个非常快速的问题...

我正在使用 nlohmann 的 json 库。

我的问题是当我打印一个元素时,程序停止响应。

我的 json 文件

{
  "Consoleprinting": false,
  "Input" : [{"Code" : [{"Name": "EC", "Keybind": "VK_NUMPAD1"}] }]
}

我试过的。

nlohmann::json jsonData = nlohmann::json::parse(i_Read);

std::cout << jsonData << std::endl;

for (auto& array : jsonData["Input"]) {
    std::cout << array["Code"] << std::endl;
}

^ 这有效但打印出来

[{"Name": "EC", "Keybind": "VK_NUMPAD1"}]

我怎样才能让它打印出名字?

array["Code"] 是一个包含单个 key-value 对集合的数组,所以你需要这样写:

std::cout << array["Code"][0]["Name"] << std::endl;