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. [ 转载 ] Python Web 框架:Django、Flask 与 Tornado 的性能对比

    本文的数据涉及到我面试时遇到过的问题,大概一次 http 请求到收到响应需要多少时间.这个问题在实际工作中与框架有比较大的关系,因此特别就框架的性能做了一次分析. 这里使用 2016 年 6 月 9 ...

  2. BZOJ.2111.[ZJOI2010]排列计数(DP Lucas)

    题目链接 对于\(a_i>a_{i/2}\),我们能想到小根堆.题意就是,求构成大小为\(n\)的小根堆有多少种方案. 考虑DP,\(f[i]\)表示构成大小为\(i\)的小根堆的方案数,那么如 ...

  3. Good Bye 2016 F.New Year and Finding Roots(交互)

    题目链接 \(Description\) 有一棵高度为\(h\)的满二叉树,点从\(1\)到\(2^h-1\)编号(无序).每次你可以询问一个点的编号,交互库会返回其所有邻接点的编号.你需要在\(16 ...

  4. SDOI2013 R1 Day1

    目录 2018.3.22 Test 总结 T1 BZOJ.3122.[SDOI2013]随机数生成器(BSGS 等比数列) T2 BZOJ.3123.[SDOI2013]森林(主席树 启发式合并) T ...

  5. 如何使用PhoneGap打包Web App

    最近做了一款小游戏,定位是移动端访问,思来想去最后选择了jQuery mobile最为框架,制作差不多以后,是否可以打包成App,恰好以前对PhoneGap有耳闻,便想用这个来做打包,可以其中艰辛曲折 ...

  6. BZOJ4276 : [ONTAK2015]Bajtman i Okrągły Robin

    建立线段树, S向每个叶子连边,容量1,费用0. 孩子向父亲连边,容量inf,费用0. 每个强盗向T连边,容量1,费用为c[i]. 对应区间内的点向每个强盗,容量1,费用0. 求最大费用流即可. #i ...

  7. Codeforces Round #369 (Div. 2) E. ZS and The Birthday Paradox 数学

    E. ZS and The Birthday Paradox 题目连接: http://www.codeforces.com/contest/711/problem/E Description ZS ...

  8. Docker系列之(五):使用Docker Compose编排容器

    1. 前言 Docker Compose 是 Docker 容器进行编排的工具,定义和运行多容器的应用,可以一条命令启动多个容器. 使用Compose 基本上分为三步: Dockerfile 定义应用 ...

  9. 2015 年度新增开源软件排名 TOP 100 - 开源中国社区

    2015 年度新增开源软件排名 TOP 100 - 开源中国社区 39.ABTestingGateway http://www.oschina.net/news/69808/2015-annual-r ...

  10. Booting LPC-Link2, Updating LPCXpresso firmware

    Booting LPC-Link2 The recommended way to use LPC-Link2 with the LPCXpresso IDE is to boot and soft l ...