使用Jsoncpp代码比较简单,各个类都容易理解,这里说一下遍历这个Json数据的方式。源码中直接提供了测试代码,D:\code\jsoncpp-src-0.6.0-rc2\src\jsontestrunner\main.cpp,里面printValueTree就是遍历函数,下面贴代码
fout:输出设备,可以是控制台或文件
Value:这个是Json数据解码后的基本单元,或者节点
path: 各层节点的分级字符串
代码很简单,递归输出成字符串,只有一个地方解释一下,Json::Value::Member 就是 vector<string> ,可以从value类中看到typedef std::vector<std::string> Json::Value::Members
static void
printValueTree( FILE *fout, Json::Value &value, const std::string &path = "." )
{
switch ( value.type() )
{
case Json::nullValue:
fprintf( fout, "%s=null\n", path.c_str() );
break;
case Json::intValue:
fprintf( fout, "%s=