我正在与 cpp 合作构建一个项目。
我的项目需要一个文件来进行一些配置,我决定使用 JSON 格式的文件。这是一个例子:
{
"agentname":"agent1",
"server":[
{"ip":"192.168.0.1"},
{"port":"9999"}
]
}
现在我需要阅读这个文件,所以我使用 JSON_Spirit。这是我的代码:
ifstream conf("config", ios::in);
json_spirit::mValue mvalue;
json_spirit::read(conf, mvalue);
json_spirit::mObject obj = mvalue.get_obj();
string agentname = obj.find("agentname")->second.get_str();
在代码之后,我可以得到agentname
.
但我不知道如何获得ip
和port
。
我试过这样:
string ip = obj.find("server")->second.find("ip")->second.get_str();
我认为它应该是这样的,但上面的代码不起作用。