我正在尝试自动化输出加州大学伯克利分校大学的广泛学科的过程。我缩短了代码和JSON数据,使问题更加清晰。它以JSON格式存储,我使用jsoncpp api处理数据。 json数据如下:
{ "Berkeley": {
"1" :
{
"Broad Discipline": "Agriculture & Natural Resources",
"College/School**": "Letters & Science",
"Major": "ENVIRONMENTAL ECONOMICS & POLICY",
"Admit GPA Range": "3.63 - 3.94",
"Admit Rate": "0.342105263",
"Enroll GPA Range": "3.65 - 3.94",
"Yield Rate": "0.730769231",
"Admits": "26",
"Apps": "76",
"Enrolls": "19"
},
"2" :
{
"Broad Discipline": "Agriculture & Natural Resources",
"College/School**": "Natural Resources",
"Major": "CONSERVATION & RESOURCE STUDIES",
"Admit GPA Range": "3.32 - 3.88",
"Admit Rate": "0.333333333",
"Enroll GPA Range": "3.30 - 3.90",
"Yield Rate": "0.818181818",
"Admits": "22",
"Apps": "66",
"Enrolls": "18"
}
}}
在这段代码之前,root是一个json :: Value并保存上面显示的所有json数据。
// for loop through school's broad disciplines
for (int i = 0; i < 2; i++)
{
string element = "\"" + to_string(i + 1) + "\"";
string broadDiscipline = (root["Berkeley"][element]["Broad Discipline"]).asString();
cout << "Broad Discipline: " << broadDiscipline << endl;
}
我希望它输出:
Broad Discipline: "Agriculture & Natural Resources"
Broad Discipline: "Agriculture & Natural Resources"
实际结果是:
Broad Discipline:
Broad Discipline:
我不知道我做错了什么。我也找不到jsoncpp中的文档,无论在命令中调用什么函数,如:root [“Berkeley”]。