本文介绍了在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;
}

//打印深度。
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(\\\
);
return true;
}

---输入---- / p>

  {
modules:[
{
config:{
bbbbbbbbbbbbbbbbb
DeviceID:这是名称,
Function:01_READ_COIL_STATUS,
Length:99,
Scan:111 ,
Type:Serve
}
},
{
config:{
position:[
13,
17
]
},
name:Modbus连接,
value:{
Baud:9600 ,
timeout:2.5
}
},
{
config:{
position:[
b
145
]
},
name:Modbus Device,
value:{
DeviceID 55
}
},
{
config:{
position:[
363,
512 $ b b
},
name:Function Something,
value:{

}
}
config:{
position:[
404,
701
]
},
name:Function
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,
: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 = $ b {type = [4],size = 0} - wires


解决方案>

你有一些错误相关,似乎没有一个伟大的处理递归或JSON的key->值本质以及如何与你使用的库相关。我没有测试这个代码,但它应该工作更好。

  void CDriverConfigurator :: PrintJSONValue 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(\\\
);
for(Json :: ValueIterator itr = root.begin(); itr!= root.end(); itr ++){
//打印深度。
for(int tab = 0; tab< depth; tab ++){
printf( - );
}
printf(subvalue();
PrintJSONValue(itr.key());
printf() - );
PrintJSONTree(* itr,depth);
}
return true;
} else {
printf();
PrintJSONValue(root);
printf(\\\
);
}
return true;
}


I have a C++ application that uses jsoncpp to decode a JSON string. I have created the following function but it only shows me the top level objects...

How do I get it to dump the 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 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"
         }
      }
   ]
}

--Output--

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

You have some errors related to seemingly not having a great handle on recursion or the key->value nature of JSON and how that relates to the library you're using. I haven't tested this code at all, but it 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::ValueIterator 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;
}

这篇关于在JsonCpp中遍历对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:23