我从curl请求中获取了一个
JSON
字符串,如下所示:
[
{
"name": "P309",
"path": "P309",
"sha": "b9dbfa7ceb9db3b73c84ff326a418bdd50c1b227",
"type": "file",
"_links": {
"self": "https://api.github.com/repos//files/contents/P309?ref=main",
"git": "https://api.github.com/repos//files/git/blobs/b9dbfdd50c1b227",
"html": "https://github.com//files/blob/main/P309"
}
},
{
"name": "P310",
"path": "P310",
"sha": "b9dbfa7ceb9db3b73c84ff326a418bdd50c1b227",
"_links": {
"self": "https://api.github.com/repos//files/contents/P309?ref=main",
"git": "https://api.github.com/repos//files/git/blobs/b9dbfdd50c1b227",
"html": "https://github.com//files/blob/main/P309"
}
},
]
如何使用 nlohmann::json lib 解析/读取它?
我找到了这个例子:
/*
"emulators": [
{
"general_info": {
"dev_id": "0123456789"
}
}
]
*/
for(json& o : data["emulators"]) {
json& gi = o["general_info"];
std::cout << gi["dev_id"] << '\n';
}
我的
JSON
被解析没有任何“标题/标题”,我对如何阅读它感到困惑。
我尝试过:
using json = nlohmann::json;
json data = json::parse(downloaded_data);
for(json& o : data[0]) {
auto git = o["git"].get<std::string>();
auto name = o["name"].get<std::string>();
}
git
正确输出值,但是,在读取 name
时,它会抛出异常。
我可以得到任何帮助吗?
JSon 似乎无效。
我尝试在 json 验证器网站上解析您的 json。在json对象中,逗号用于分隔键值对,但最后一个键值不需要包含命令,删除它,其他一切似乎都很好。检查下面的图片作为参考。 jsonlint 结果