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 );

  1. #include 定义定义
  2. #include
  3. #include "json.h"
  4. using namespace std;
  5. //定义jsoncpp 支持的对象类型
  6. enum Type
  7. {
  8. nullValue = 0, ///< 'null' value
  9. intValue,      ///< signed integer value
  10. uintValue,     ///< unsigned integer value
  11. realValue,     ///< double value
  12. stringValue,   ///< UTF-8 string value
  13. booleanValue,  ///< bool value
  14. arrayValue,    ///< array value (ordered list)
  15. objectValue    ///< object value (collection of name/value pairs).
  16. };
  17. int main()
  18. {
  19. Json::Value root;
  20. root["type"] = nullValue;
  21. root["name"] = "Xin Ma";
  22. root["pwd"] = "123456";
  23. string tmpstr = root.toStyledString();
  24. char buff[1024] = {0};
  25. strcpy_s(buff, strlen(tmpstr.c_str())+1, tmp.c_str());
  26. cout<<"buff_root :"<

C++中JSON的使用详解【转】的更多相关文章

  1. JAVA中的四种JSON解析方式详解

    JAVA中的四种JSON解析方式详解 我们在日常开发中少不了和JSON数据打交道,那么我们来看看JAVA中常用的JSON解析方式. 1.JSON官方 脱离框架使用 2.GSON 3.FastJSON ...

  2. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  3. Python中的高级数据结构详解

    这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...

  4. Asp.net中web.config配置文件详解(一)

    本文摘自Asp.net中web.config配置文件详解 web.config是一个XML文件,用来储存Asp.NET Web应用程序的配置信息,包括数据库连接字符.身份安全验证等,可以出现在Asp. ...

  5. vue-cli 中的 webpack 配置详解

    本篇文章主要介绍了 vue-cli 2.8.2 中的 webpack 配置详解, 做个学习笔记 版本 vue-cli 2.8.1 (终端通过 vue -V 可查看) vue 2.2.2 webpack ...

  6. js中中括号,大括号使用详解

    js中中括号,大括号使用详解 一.总结 一句话总结:{ } 是一个对象,[ ] 是一个数组 1.js大括号{}表示什么意思? 对象 { } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或 ...

  7. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  8. c++中vector的用法详解

    c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...

  9. 011-Scala中的apply实战详解

    011-Scala中的apply实战详解 object中的apply方法 class中的apply方法 使用方法 apply方法可以应用在类或者Object对象中 class类 必须要创建实例化的类对 ...

随机推荐

  1. mysql where 条件中的字段有NULL值时的sql语句写法

    比如你有一个sql语句联表出来之后是这样的 id           name            phone                  status 1            张三     ...

  2. 配置lambda

    =========== 添加 apply plugin: 'me.tatarka.retrolambda' 添加 compileOptions {         sourceCompatibilit ...

  3. BZOJ4816 Sdoi2017数字表格

    一开始只推出O(TN)的做法,后来看了看发现再推一步就好了. 我们只需要枚举gcd就可以啦. 然后我们改变一下枚举顺序 设T为dk 预处理中间那部分前缀积就好了. #include<bits/s ...

  4. NOIP练习赛题目2

    小K的农场 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个 ...

  5. play framework系列之maven 构建

    一,文章内容 1,Play framwwork 的好处就是构建java 项目简单快速.经过下边几步即可完成. play new testSimpleMvn 选择2 java 项目即可. 然后进入到 t ...

  6. HTML5 的新特性以及新标签的浏览器兼容问题

    新特性: HTML5 现在已经不是 SGML 的子集,主要是关于图像,位置,存储,多任务等功能的增加. 1)  拖拽释放(Drag and drop) API 2)  语义化更好的内容标签(heade ...

  7. Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

    C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...

  8. [BZOJ1115][POI2009]石子游戏Kam解题报告|阶梯博弈

    有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏.问先手是否必胜. 首先 ...

  9. OpenVPN使用easy-rsa3吊销证书

    cd /etc/easy-rsa ./easyrsa revoke targetkey(证书名) ./easyrsa gen-crl 其中gen-crl会生成一份吊销证书的名单,放在pki/crl.p ...

  10. RSS介绍、RSS 2.0规范说明和示例代码

    RSS是一种消息来源格式规范,用以发布经常更新资料的网站,例如博客.新闻的网摘.RSS文件,又称做摘要.网摘.更新.频道等,包含了全文或节选文字,再加上一定的属性数据.RSS让发布者自动发布信息,也使 ...