c++ - 遍历 JsonCpp 中的对象

标签 c++ json jsoncpp

我有一个使用 jsoncpp 的 C++ 应用程序解码 JSON 字符串。我创建了以下函数,但它只显示顶级对象...

如何让它转储整个对象列表?

--功能--

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;
}

--- 输入----

{
   "modules":[
      {
         "config":{
            "position":[
               129,
               235
            ]
         },
         "name":"Modbus Task",
         "value":{
            "DeviceID":"This is the 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"
         }
      }
   ]
}

--输出--

{type=[7], size=3} {type=[4], size=0} - modules
{type=[4], size=0} - properties
{type=[4], size=0} - wires 

最佳答案

您遇到了一些错误,这些错误与似乎没有很好地处理递归或 JSON 的 key->value 本质以及这与您正在使用的库的关系有关。我根本没有测试过这段代码,但它应该会更好。

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;
}

关于c++ - 遍历 JsonCpp 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4800605/

相关文章:

c++ - 如何在大型机中添加第二个工具栏?

c++ - 列表框属性值未出现在 C++ 自定义操作 Wix 中?

java - 如何从 JSON 输出字符串中提取各个数据片段?

c++ - 使用 libcurl 和 jsoncpp 向 django 发送 POST 请求时无效的 unicode

c++ - JSONCPP 使用 Visual Studio 2010 构建

c++ - 如何读取包含多个根元素的 JSON 文件?

c++ - C++ 'struct name' 中的结构未命名类型

c++ - 链成员初始值设定项

javascript - 按两个条件对数据进行排序?

c# - 如何在 sql 数据库的 asp.net 网页中显示 JSON 输出