I am working with cpp to build a project.
My project needs a file to do some configuration and I decided to use a file with JSON format. Here is an example:
{
"agentname":"agent1",
"server":[
{"ip":"192.168.0.1"},
{"port":"9999"}
]
}
Now I need to read this file so I use the JSON_Spirit. Here is my code:
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();
After the code, I can get agentname
.
But I don't know how to get ip
and port
.
I have tried like this:
string ip = obj.find("server")->second.find("ip")->second.get_str();
I think it should be something like this but the code above doesn't work.
I find that with json_spirit it helps to have a few utility accessor functions. Also, take care to examine the actual contents of the document:
This will work:
expected output: