C++中JSON的使用详解【转】
https://blog.csdn.net/admin_maxin/article/details/53175779
jsoncpp 主要包含三个class:Value、Reader、Writer。注意Json::Value 只能处理 ANSI 类型的字符串,如果 C++ 程序是用 Unicode 编码的,最好加一个 Adapt 类来适配。
Json内部类和方法:
Reader<是用于读取的,说的确切点,是用于将字符串转换为 Json::Value 对象的>
【构造函数】
1、Reader();
【拷贝构造函数】
2、Reader( const Features &features );
【将字符串或者输入流转换为JSON的Value对象】
【下边是相应的parse的重载函数】
3、bool parse( const std::string &document, Value &root,bool collectComments = true );
4、bool parse( const char *beginDoc, const char *endDoc,
Value &root,bool collectComments = true
5、bool parse( std::istream &is,Value &root,bool collectComments = true );
6、std::string getFormatedErrorMessages() const;
Value:<是jsoncpp 中最基本、最重要的类,用于表示各种类型的对象,jsoncpp 支持的对象类型可见 Json::ValueType 枚举值; Value类的对象代表一个JSON值,既可以代表一个文档,也可以代表 文档中一个值。如同JSON中定义的“值”一样,Value是递归的>
【构造函数】
1、Value( ValueType type = nullValue );
Value( Int value );
Value( UInt value );
Value( double value );
Value( const char *value );
Value( const char *beginValue, const char *endValue );
【拷贝构造函数】
2、Value( const StaticString &value );
Value( const std::string &value );
Value( const Value &other );
【相同类型的比较、交换、类型的获取】
3、void swap( Value &other );
ValueType type() const;
int compare( const Value &other );
【相应的赋值运算符的重载函数】
4、Value &operator=( const Value &other );
bool operator <( const Value &other ) const;
bool operator <=( const Value &other ) const;
bool operator >=( const Value &other ) const;
bool operator >( const Value &other ) const;
bool operator ==( const Value &other ) const;
bool operator !=( const Value &other ) const;
bool operator!() const;
Value &operator[]( UInt index );
const Value &operator[]( UInt index ) const;
【将Value对象进行相应的类型转换】
5、const char *asCString() const;
std::string asString() const;
const char *asCString() const;
std::string asString() const;
Int asInt() const;
UInt asUInt() const;
double asDouble() const;
【相应的判断函数】
6、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;
bool isConvertibleTo( ValueType other ) const;
bool isValidIndex( UInt index ) const;
bool isMember( const char *key ) const;
bool isMember( const std::string &key ) const;
【清除和扩容函数】
7、void clear();
void resize( UInt size );
【获取满足相应条件的Value】
8、Value get( UInt index, const Value &defaultValue ) const;
Value get( const std::string &key,const Value &defaultValue ) const;
Members getMemberNames() const;
【删除满足相应条件的Value】
9、Value removeMember( const char* key );
Value removeMember( const std::string &key );
10、void setComment( const char *comment,CommentPlacement placement );
void setComment( const std::string &comment,CommentPlacement placement );
bool hasComment( CommentPlacement placement ) const;
std::string getComment( CommentPlacement placement ) const;
std::string toStyledString()const;
Writer:<类是一个纯虚类,并不能直接使用。在此我们使用 Json::Writer 的子类(派生类):
Json::FastWriter、Json::StyledWriter、Json::StyledStreamWriter。顾名思义,用
Json::FastWriter 来处理 json
应该是最快的;负责将内存中的Value对象转换成JSON文档,
输出到文件或者是字符串中>
【FastWriter】
1、FastWriter();
virtual ~FastWriter(){}
void enableYAMLCompatibility();
virtual std::string write( const Value &root );
【StyledWriter】
2、StyledWriter();
virtual ~StyledWriter(){}
virtual std::string write( const Value &root );
- #include 定义定义
- #include
- #include "json.h"
- using namespace std;
- //定义jsoncpp 支持的对象类型
- enum Type
- {
- nullValue = 0, ///< '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).
- };
- int main()
- {
- Json::Value root;
- root["type"] = nullValue;
- root["name"] = "Xin Ma";
- root["pwd"] = "123456";
- string tmpstr = root.toStyledString();
- char buff[1024] = {0};
- strcpy_s(buff, strlen(tmpstr.c_str())+1, tmp.c_str());
- cout<<"buff_root :"<
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-04-10 RenderMonkey 练习 第二天 【opengl 光照模型】