在JsonCpp中迭代对象 我有一个C++应用程序,用来解码JSON字符串。我创建了以下函数,但它只显示顶级对象
如何让它转储整个对象列表 --功能--在JsonCpp中迭代对象 我有一个C++应用程序,用来解码JSON字符串。我创建了以下函数,但它只显示顶级对象,c++,json,jsoncpp,C++,Json,Jsoncpp,如何让它转储整个对象列表 --功能-- 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 ) {
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;
}
{type=[7], size=3} {type=[4], size=0} - modules
{type=[4], size=0} - properties
{type=[4], size=0} - wires
--输出--
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;
}
{type=[7], size=3} {type=[4], size=0} - modules
{type=[4], size=0} - properties
{type=[4], size=0} - wires
您可能会遇到一些错误,这些错误与似乎没有很好地处理递归或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;
}
void CDriverConfigurator::PrintJSONValue(const Json::Value&val)
{
if(val.isString()){
printf(“字符串(%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();
}
其他的
{
printf(“未知类型=[%d]”,val.type();
}
}
bool-CDriverConfigurator::PrintJSONTree(const-Json::Value&root,无符号短深度/*=0*/)
{
深度+=1;
printf(“{type=[%d],size=%d}”,root.type(),root.size());
如果(root.size()>0){
printf(“\n”);
for(Json::Value::const_迭代器itr=root.begin();itr!=root.end();itr++){
//打印深度。
对于(int-tab=0;tab
如果您只是想打印Json::Value,那么有一个解决方案:
Json::Value val;
/*...build the value...*/
cout << val.toStyledString() << endl;
Json::Value val;
/*…建立价值*/
cout有一种简单的方法可以迭代json::value中的所有字段。我省略了printf的内容
#包括“cpprest/json.h”
#包括“cpprest/filestream.h”
使用web::json::value;
使用std::wstring;
静态void printOneValue(常量wstring和key、常量double和value);
静态void printOneValue(常量wstring和key、常量bool和value);
静态void printOneValue(常量wstring和key、常量int和value);
静态void printOneValue(const wstring&key、const wstring&value);
静态void printOne(常量wstring和key、常量值和v、_numlevel);
静态无效打印树(常量值和v);
静态无效打印树(常量值和v)
{
如果(!v.is_object())
返回;
尝试
{
printOne(wstring(),v,0);
}
捕获(…)
{
//错误处理
}
}
静态void printOne(常量wstring和key、常量值和v、_numlevel)
{
开关(v.type())
{
案例值::值\类型::编号:
如果(v.is_double())
printOneValue(key,v.as_double());
其他的
printOneValue(key,v.as_integer());
打破
大小写值::值类型::布尔值:
printOneValue(key,v.as_bool());
打破
大小写值::值\类型::字符串:
printOneValue(key,v.as_string());
打破
案例值::值\类型::对象:
用于(自动iter:v.as_object())
{
const wstring&k=iter.first;
常数值&val=iter.second;
打印一(k,val,级别+1);
}
打破
大小写值::值类型::数组:
用于(自动it:v.as_数组())
{
printOne(按键、it、级别+1);
}
打破
大小写值::值\类型::空:
违约:
打破
}
}
静态void printOneValue(const wstring&key、const wstring&value)
{
//处理你的关键和价值
}
静态void printOneValue(常量wstring和key、常量int和value)
{
//处理你的关键和价值
}
静态void printOneValue(常量wstring和key、常量double和value)
{
//处理你的关键和价值
}
静态void printOneValue(常量wstring和key、常量bool和value)
{
//处理你的关键和价值
}
这是一个很好的示例,可以打印json
对象和对象成员(及其值):
给定成员名称,获取值
// 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;
}
//打印data1值下的所有项目
vector memberNames=root[“test1”][“data1”]。getMemberNames();
for(常量字符串&mn:memberNames)
{
这会产生我想要的输出吗?但我做这个练习是为了学习如何遍历JSON树。感谢我真正想要的东西。谢谢。@Steven smethurst:真正有趣的是,我以前从来没有看过这个库。:-@cegprakash:为什么删除“reference”标记?仅供参考这是一种使用getMemberNames()的方法这里可以找到路径:@Omnifarious:实际上,Json::ValueIterator
是一个typedef。还有Json::ValueIterator
typedef。因此,正确的更改可能是将Json::ValueIterator
替换为Json::ValueIterator
。GCC建议在错误消息中使用Json::Value::const\u iterator
这很可能是建议编辑的原因。虽然这段代码可能会回答这个问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。请阅读这篇文章以提供高质量的答案。这里的示例代码甚至不是基于JsonCpp,而是一个完全不同的库!这是一个完全不同的问题的答案。
// 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;
}