Search code examples
c++jsonjsoncpp

Retrieving a JSON object within another one


I have a simple JSON format text file and I am having some trouble retrieving values in the "pixel" array. Here is the file:

{ "luminaire" : 
  { "sensors": 
     { "pixel" : [2000,2001,2002] } 
  } 
}

The code I wrote in order to do this is the following:

//After parsing success...    
Json::Value pixel = root_["luminaire"].get("sensors" , "nothing").get("pixel" , "nopixel");
int value = pixel[0].asInt();

I tried many ways to do it but I keep getting the following error:

terminate called after throwing an instance of 'Json::LogicError'
what():  in Json::Value::operator[](ArrayIndex): requires arrayValue

I also tried

Json::Value:ArrayIndex and root[0]

but I get the same error.
How can I retrieve the values in the "pixel" array?


Solution

  • Don't you mean root_.get("luminaire")? After all, luminaire is a key name just like sensors.