0

If I have a json file:

{ "key1" : "value1", "key2" : "value2" }

using:

Json::Value root;
Json::Reader reader;

std::ifstream in(filePath.c_str());

if (!in)
{
    std::string errReason("Cannot open the settings file '");

    errReason += filePath;
    errReason += "'";

    throw std::domain_error(errReason);
}


bool parsingSuccessful = reader.parse(in, root);

if (!parsingSuccessful)
{
    std::cout << "Error parsing the string" << std::endl;
}
else
{
    std::cout << "Works" << std::endl;
}

Json::Value::Members names = root.getMemberNames();

for (int index = 0; index < names.size(); ++index)
{
    std::string key = names[index].asString();
    std::string value = root[key].asString();
    map_.insert(make_pair(key, value));
    std::cout << root[index] << std::endl;
}

i cannot seem to assign any value to variable string 'key'.

Can anyone explain what i am doing wrong?

azuric
  • 2,679
  • 7
  • 29
  • 44

1 Answers1

2

The type of the variable 'names' is a Json::Value::Members, which is a vector of string (std::vector<std::string>) and is not a Json::Value, so you don't have to add asString() method.

so if you want modify the content of the 'key' value in 'root':

for (int index = 0; index < names.size(); ++index)
{
    std::string key = names[index];
    root[key] = value ;
    std::cout << root[key] << std::endl;
}

if you want just to read the json file and store its values in a map:

for (int index = 0; index < names.size(); ++index)
{
    std::string key = names[index];
    std::string value = root[key].asString();
    map_.insert(make_pair(key, value));
    std::cout << root[key] << std::endl;
}