json-cpp 库使用
来源:互联网 发布:单片机是ic吗 编辑:程序博客网 时间:2024/05/21 06:23
Json-cpp
是一个使用c++开发的面向对象的json库。
核心数据结构
Reader是json-cpp数据的读取器,可以从文件流,字符串中读取json数据,使用下面的接口:
boolparse( const std::string &document,
Value &root,
bool collectComments = true);
boolparse( std::istream &is,
Value &root,
bool collectComments = true);
将所有的json解析到Value结果里面。
Value是json解析的结果,它通过key-value的方式用来代表整数、浮点数、UTF-8的字符串、或者是一个value数组等,这个与json的格式定义保持一致。它的类型可以通过type获取,type的定义如下:
/*
* brief Type of the value held by a Value object.
*/
enum ValueType
{
nullValue = 0, ///< 'null' value
intValue, ///< signedinteger value
uintValue, ///< unsignedinteger value
realValue, ///< double value
stringValue, ///< UTF-8string value
booleanValue, ///< bool value
arrayValue, ///< array value(ordered list)
objectValue ///< objectvalue (collection of name/value pairs).
};
Value访问的常用接口:
const char *asCString() const;
std::string asString() const;
# ifdef JSON_USE_CPPTL
CppTL::ConstString asConstString() const;
# endif
Int asInt() const;
UInt asUInt() const;
Int64 asInt64() const;
UInt64 asUInt64() const;
LargestInt asLargestInt() const;
LargestUInt asLargestUInt() const;
float asFloat() const;
double asDouble() const;
bool asBool() const;
bool isNull() const;
bool isBool() const;
bool isInt() const;
bool isUInt() const;
bool isIntegral() const;
bool isDouble() const;
bool isNumeric() const;
bool isString() const;
bool isArray() const;
bool isObject() const;
/// Return true if the object has a member named key.
bool isMember( const char *key ) const;
/// Return true if the object has a member named key.
bool isMember( const std::string &key ) const;
# ifdef JSON_USE_CPPTL
/// Return true if the object has a member named key.
bool isMember( const CppTL::ConstString &key ) const;
# endif
/// \brief Return a list of the member names.
///
/// If null, return an empty list.
/// \pre type() is objectValue or nullValue
/// \post if type() was nullValue, it remains nullValue
Members getMemberNames() const;
使用方法
#include "json/json.h"
#include "iostream"
#include "string"
int main()
{
std::string strValue ="[{\"name\":\"TOM\",\"id\":4},{\"name\":\"JACK\",\"id\":5}]";
Json::Reader reader;
Json::Value value;
if(reader.parse(strValue, value))
{
if (value.type() != Json::arrayValue) {
printf("Json type is not arrayValue %d\n", value.type());
return 0;
}
for(unsigned int i = 0;i < value.size(); i++)
{
if (value[i].type() != Json::objectValue) {
printf("Json type is not objectValue %d\n", value[i].type());
continue;
}
if(!value[i].isMember("name"))
continue;
std::string name = value[i]["name"].asString();
std::cout << "got name " << name <<std::endl;
if(!value[i].isMember("id"))
continue;
int id = value[i]["id"].asInt();
std::cout << "got id" << id << std::endl;
Json::Value::Members member = value[i].getMemberNames();
for(Json::Value::Members::iterator iter = member.begin(); iter !=member.end(); ++iter)
{
std::cout << "gotmember " << (*iter) << std::endl;
}
}
}
return 0;
}
- json-cpp 库使用
- json for cpp库使用介绍2
- c++使用Protobuf Message转Json字符串(Json库使用Json cpp)
- exportjson2cpp 使用(coco2dx json文件转CPP)
- vc6.0使用Json-cpp的配置方法
- json cpp 的安装
- json cpp 的简单操作
- 使用Dev-Cpp编译Boost库
- Activemq cpp 使用记录
- 初次使用zxing cpp
- activeMq-CPP的使用
- dev-cpp使用教程
- gRPC的使用(cpp)
- socketio-cpp-lib使用
- json-cpp longlong 类型的扩展
- cpp的json的用法举例
- JSON解析 - java库org.json使用
- 使用ROS的cpp客户端库写程序
- 黑马程序员_高新技术:JDK1.5新特性
- 搜狗开源内部项目管理平台Cynthia意欲何为
- 数据库事务
- 要多动脑子,要追求效率
- HDU 2222 - Keywords Search (AC自动机)
- json-cpp 库使用
- query显示与隐藏实例
- POJ 1947 Rebuilding Roads(动态规划-树形DP)
- Python3.3 print readline 乱码
- POJ 3259--Wormholes(求负环)
- Jackson解析首字母大写的json字符串
- Android开发图形处理创建一个图形的拷贝
- XML笔记
- kmp算法--求字符串子串--《数据结构》严蔚敏