遍历 JsonCpp 中的对象

新手上路,请多包涵

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

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

- 功能 -

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

原文由 Steven Smethurst 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

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

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

原文由 Omnifarious 发布,翻译遵循 CC BY-SA 3.0 许可协议

给定成员名称,获取值

// Print all items under data1 value
vector<string> memberNames = root["test1"]["data1"].getMemberNames();
for (const string& mn : memberNames)
{
    cout << "[" << mn << "]:" << "[" << root["test1"]["data1"].get(mn, "None") << "]" << endl;
}

原文由 HandyManDan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题