The JSON file looks like this:
{
"strings": [
{
"key_one": "value_one!"
},
{
"key_two": "value_two!"
},
]
}
The C++ file looks like this:
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(contents, root);
const Json::Value strings = root["strings"];
std::vector<std::string> list = strings.getMemberNames();
The error caused by “strings.getMemberNames()” is:
Assertion failed: (type_ == nullValue || type_ == objectValue), function getMemberNames, file /projects/.../jsoncpp.cpp,
strings is an arrayValue, I confirmed it by getting it’s ValueType = 6.
As you say, strings is an array, not an object. You need to either : (i) convert your strings json to be an object.
In which case your existing code will be fine. This is what I’d do if you have control over the json you’re parsing.
or (ii) Iterate over the strings array – I’d only do this if the json is specified by some thrid party – It will look something like this:
However actually using the values in all_keys to access anything in the strings array later is going to be painful – so you might want to store the key-value pairs in a map.
Or at least store the index of the strings array where the key was found.