c++ - Iterating through objects in JsonCpp -
i have c++ application uses jsoncpp decode json string. have created following function shows me top level objects...
how dump entire object list?
--function--
savejson( json_data ); bool cdriverconfigurator::printjsontree( json::value & root, unsigned short depth /* = 0 */) { printf( " {type=[%d], size=%d} ", root.type(), root.size() ); if( root.size() > 0 ) { for( json::valueiterator itr = root.begin() ; itr != root.end() ; itr++ ) { printjsontree( itr.key(), depth+1 ); } return true; } // print depth. for( int tab = 0 ; tab < depth; tab++) { printf( "-"); } if( root.isstring() ) { printf( " %s", root.asstring().c_str() ); } else if( root.isbool() ) { printf( " %d", root.asbool() ); } else if( root.isint() ) { printf( " %d", root.asint() ); } else if( root.isuint() ) { printf( " %d", root.asuint() ); } else if( root.isdouble() ) { printf( " %f", root.asdouble() ); } else { printf( " unknown type=[%d]", root.type() ); } printf( "\n" ); return true; }
--- input ----
{ "modules":[ { "config":{ "position":[ 129, 235 ] }, "name":"modbus task", "value":{ "deviceid":"this name", "function":"01_read_coil_status", "length":"99", "scan":"111", "type":"serve" } }, { "config":{ "position":[ 13, 17 ] }, "name":"modbus connection", "value":{ "baud":"9600", "timeout":"2.5" } }, { "config":{ "position":[ 47, 145 ] }, "name":"modbus device", "value":{ "deviceid":"55" } }, { "config":{ "position":[ 363, 512 ] }, "name":"function something", "value":{ } }, { "config":{ "position":[ 404, 701 ] }, "name":"function something", "value":{ } } ], "properties":{ "blarrg":"", "description":"", "name":"modbus" }, "wires":[ { "src":{ "moduleid":1, "terminal":"modbus.connection.output" }, "tgt":{ "moduleid":2, "terminal":"modbus.connection.input" } }, { "src":{ "moduleid":2, "terminal":"modbus.device.output" }, "tgt":{ "moduleid":0, "terminal":"modbus.device.output" } }, { "src":{ "moduleid":3, "terminal":"dataout" }, "tgt":{ "moduleid":4, "terminal":"datain" } }, { "src":{ "moduleid":3, "terminal":"datain" }, "tgt":{ "moduleid":0, "terminal":"data1" } } ] }
--output--
{type=[7], size=3} {type=[4], size=0} - modules {type=[4], size=0} - properties {type=[4], size=0} - wires
you have errors related seemingly not having great handle on recursion or key->value nature of json , how relates library you're using. haven't tested code @ all, should work better.
void cdriverconfigurator::printjsonvalue( const json::value &val ) { if( val.isstring() ) { printf( "string(%s)", val.asstring().c_str() ); } else if( val.isbool() ) { printf( "bool(%d)", val.asbool() ); } else if( val.isint() ) { printf( "int(%d)", val.asint() ); } else if( val.isuint() ) { printf( "uint(%u)", val.asuint() ); } else if( val.isdouble() ) { printf( "double(%f)", val.asdouble() ); } else { printf( "unknown type=[%d]", val.type() ); } } bool cdriverconfigurator::printjsontree( const json::value &root, unsigned short depth /* = 0 */) { depth += 1; printf( " {type=[%d], size=%d}", root.type(), root.size() ); if( root.size() > 0 ) { printf("\n"); for( json::value::const_iterator itr = root.begin() ; itr != root.end() ; itr++ ) { // print depth. for( int tab = 0 ; tab < depth; tab++) { printf("-"); } printf(" subvalue("); printjsonvalue(itr.key()); printf(") -"); printjsontree( *itr, depth); } return true; } else { printf(" "); printjsonvalue(root); printf( "\n" ); } return true; }
Comments
Post a Comment