4

I'm using the Json-Spirit library, however i'm unsure how to read value from an object, without iterating over each of the name-value pairs.

If i have an object such that:

{
    "boids":
    {
        "width": 10,
        "count": 5,
        "maxSpeedMin": 2,
        "maxSpeedMax": 80,
        "maxForceMin": 0.5,
        "maxForceMax": 40
    }
}

How can I access, for example, the width value by name?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Onedayitwillmake
  • 711
  • 1
  • 10
  • 19

2 Answers2

4

json_spirit added support for std::map so that you can look up a value.

One of the projects in the json_spirit download is json_map_demo. This will help you to understand it better.

Gautam Jain
  • 6,789
  • 10
  • 48
  • 67
3

This is possible.

A sample code below.

string test = {
"boids":
 {
   "width": 10,
   "count": 5,
   "maxSpeedMin": 2,
   "maxSpeedMax": 80,
   "maxForceMin": 0.5,
   "maxForceMax": 40
 }
}

mValue value;
if(read(test, value))
{
 mObject obj = value.get_obj();
 obj = obj.find("boids")->second.get_obj();
/*Now the obj would contain the sub object,that is 
{"width": 10,
 "count": 5,
 "maxSpeedMin": 2,
 "maxSpeedMax": 80, 
 "maxForceMin": 0.5,
 "maxForceMax": 40 
}
*/

int nWidth =  obj.find("width")->second.get_int();
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
S_R
  • 493
  • 1
  • 9
  • 22
  • well, "obj = obj.find("boids")->second.get_obj();" gives an error in run time. You need to define a new mObject. – henryyao Jun 09 '14 at 20:03