jsoncpp用法简述

Jsoncpp是一个使用C++语言实现的面向对象的json库。

Jsoncpp提供的接口中有3个核心类:ReaderWriterValue。

Reader类负责从字符串或者输入流中加载JSON文档,并进行解析,生成代表JSON档的Value对象。

Writer类负责将内存中的Value对象转换成JSON文档,可输出到文件或者是字符串中。

Value类的对象代表一个JSON值,既可以代表一个文档,也可以代表文档中一个值。



一个JSON文档的大致过程如下:

//准备Json源数据,如读取文档:Std::stringstrdoc=readFromFile(… );

.........

//生命顶级Value对象

Json::Valueroot;

//声明Reader对象

Json::Reader_reader;

//解析json文档

_reader.paser(strdoc,root);

Json::ValueType有8种,以下是定义。 enum Json::ValueType

Enumerator:

nullValue 'null' value

intValue signed integer value

uintValue unsigned integer value

realValue double value

stringValue UTF-8 string value.

booleanValue bool value

arrayValue array value (ordered list)

objectValue object value (collection of name/value pairs).

static void printValueTree( FILE *fout, Json::Value &value, const std::string &path = "." ) 
{ 
switch ( value.type() ) 
{ 
case Json::nullValue:    //还可以通过isNull()判断其值: if(!value["id"].isNull()) 。。。
    fprintf( fout, "%s=null\n", path.c_str() );
    break;
case Json::intValue:
    fprintf( fout, "%s=%d\n", path.c_str(), value.asInt() );
    break;
case Json::uintValue:
    fprintf( fout, "%s=%u\n", path.c_str(), value.asUInt() );
    break;
case Json::realValue:
    fprintf( fout, "%s=%.16g\n", path.c_str(), value.asDouble() );
    break;
case Json::stringValue:
    fprintf( fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str() );
    break;
case Json::booleanValue:
    fprintf( fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false" );
    break;
case Json::arrayValue:
{
    fprintf( fout, "%s=[]\n", path.c_str() );
    int size = value.size();
    for ( int index =0; index < size; ++index )
    {
        static char buffer[16];
        sprintf( buffer, "[%d]", index );
        printValueTree( fout, value[index], path + buffer );
    }
}
break;
case Json::objectValue:
{
    fprintf( fout, "%s={}\n", path.c_str() );
    Json::Value::Members members( value.getMemberNames() );
    std::sort( members.begin(), members.end() );
    std::string suffix = *(path.end()-1) == '.' ? "" : ".";
    for ( Json::Value::Members::iterator it = members.begin(); it != members.end();         ++it )
    {
        const std::string &name = *it;
        printValueTree( fout, value[name], path + suffix + name );
    }
}
break;
default:
    break; 
} 
}

1) Json::Reader 是用于读取Json对象的值。

用法:

Json::Value reader_object;

Json::Reader reader;

const char* reader_document = "{"path" : "/home/test.mp3","size" : 4000}";

if (!reader.parse(reader_document, reader_object))

return 0;

std::cout << reader_object["path"] << std::endl;

std::cout << reader_object["size"] << std::endl;

结果:

"/home/test.mp3"

4000



2) 增加子节点

Json::Value root;

Json::Value leaf;

...

root["leaf_node"] = leaf;

3) 值为数组的,通过对同一key逐个append方式追加:

root["key_array"].append("the string"); //元素值类型为字符串

root["key_array"].append(20); //元素值类型同时可为int等等

4) 解析数组值

JArray = root["key_array"];

for ( unsigned int i = 0; i < JArray.size(); i++ )

{

cout << "JSON array values: " << JArray[i].asString() << endl;

}

二. 通过使用Writer将Value转换为JSON文档(string):

1) Json::FastWriter用来快速输出Json对象的值,即

用法:

Json::FastWriter writer;

std::cout << writer.write(json_media)<< std::endl;

结果:

{"isArray":["test1","test2"],"isBoolean":true,"isDouble":0.25,"size":4000,"isObject": {},"path":"/home/mp3/test.mp3"}


2) Json::StyledWriter用来格式化输出Json对象的值。

用法:

Json::StyledWriter writer;

std::cout << writer.write(json_media) << std::endl;

结果:

{

"isArray" : [ "test1", "test2" ],

"isBoolean" : true,

"isDouble" : 0.24,

"size" : 4000,

"isObject" : {},

"path" : "/home/mp3/test.mp3"

}

原文链接: https://www.cnblogs.com/nuaa/archive/2013/05/16/3082423.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/88809

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午11:48
下一篇 2023年2月9日 下午11:48

相关推荐