P i*_*P i 5 c++ json list unpack nlohmann-json
我正在使用https://github.com/nlohmann/json
这很棒。
但是..有什么办法可以解压:
{
"my_list" : [1,2,3]
}
Run Code Online (Sandbox Code Playgroud)
成std:vector<int>?
我找不到任何提及,并且std::vector<int> v = j["my_list"];失败了,就像j["my_list"].get<std::vector<int>>()
喵
所以它确实有效。我没有隔离测试用例,我的 JSON 字符串格式错误。
所以,
json J(json_string);
J["my_list"].get<std::vector<int>>()
Run Code Online (Sandbox Code Playgroud)
做有效。
在我的情况下,我确保我的 C++ var-names 匹配 JSON 键,所以我可以简单地使用宏:
#define EXTRACT(x) x = J[#x].get< decltype(x) >()
int foo;
std::vector<float> bar;
EXTRACT(foo);
EXTRACT(bar);
Run Code Online (Sandbox Code Playgroud)