jsoncpp用法简述
来源:互联网 发布:hadoop基础教程 python 编辑:程序博客网 时间:2024/06/04 17:40
Jsoncpp是一个使用C++语言实现的面向对象的json库。
Jsoncpp提供的接口中有3个核心类:Reader、Writer、Value。
Reader类负责从字符串或者输入流中加载JSON文档,并进行解析,生成代表JSON文档的Value对象。
Writer类负责将内存中的Value对象转换成JSON文档,可输出到文件或者是字符串中。
Value类的对象代表一个JSON值,既可以代表一个文档,也可以代表文档中一个值。
一个JSON文档的大致过程如下:
//准备Json源数据,如读取文档:Std::string strdoc = readFromFile(… );
。。。
//生命顶级Value对象
Json::Value root;
//声明Reader对象
Json::Reader _reader;
_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"
}
- jsoncpp用法简述
- jsoncpp用法
- Jsoncpp用法
- jsoncpp用法详解
- JSONCPP
- jsoncpp
- jsoncpp
- JsonCpp
- jsoncpp
- sizeof用法简述
- JOIN用法简述
- synchronized关键字用法简述
- PerformanceCounter简述及用法
- PerformanceCounter简述及用法
- 简述amixer及其用法
- 简述amixer及其用法
- 简述amixer及其用法
- 简述amixer及其用法
- Media Player Classic - HC 源代码分析 2:核心类 (CMainFrame)(1)
- Android 深入ViewPager补间动画,实现类京东商城首页广告Banner切换效果
- 用户只愿为“体验”付钱【傅盛】
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服 务器。请验证实例名称是否正确并且 SQL Server 已配置为允
- Linux工具大全_Automake
- jsoncpp用法简述
- TOMCAT内存溢出研究
- CSS优先级计算
- 从零开始编写自己的JavaScript框架(二)
- 线程函数分析
- android平台的ocr
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记五
- Leetcode: Remove Duplicates from Sorted Array II
- POJ 1066 Treasure Hunt