5

I need to read a json file with jsoncpp library.

I have this file:

{"one":false,"two":[{"id":"first"},{"id":"second"}],"three":550}

If i need to read only the first id of "two" element, i use:

std::string contents; //Contain the file
Json::Value root;
Json::Reader reader;
reader.parse(contents, root, false);
std::string aux = root["two"][0]["id"].asString();

It works fine in Linux, but when i try it in Windows i have this error:

error: ambiguous overload for 'operator[]' in 'root.Json::Value::operator[](((const char*)"two"))[0]'

Why it happens and how can i fix this? Thanks.

Solved: There are two operator[], one with an int as parameter and other with a const char as parameter, and the compiler doesn't know who to use in Windows, but yes in Linux. Now i use [0u] instead [0] to indicate a number as parameter and it works fine.

Safej
  • 73
  • 10

1 Answers1

4

This drove me crazy, until I stumbled upon this question, I mysteriously figured it out: Just cast it to Json::Uint (for some reason), or use 'u':

MapEvent::MapEvent(Json::Value& val)
{
    operator_enum = val.get(0u, "").asString();
}
Mazyod
  • 22,319
  • 10
  • 92
  • 157
  • It's annoying and probably an API mistake, but at least it's mentioned [in the docs](http://open-source-parsers.github.io/jsoncpp-docs/doxygen/class_json_1_1_value.html#af9e02b38f4e63e491c300c20b275bdd7). – cdunn2001 Feb 15 '15 at 19:26