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. ARP协议详解之Gratuitous ARP(免费ARP)

    ARP协议详解之Gratuitous ARP(免费ARP) Gratuitous ARP(免费ARP) Gratuitous ARP也称为免费ARP,无故ARP.Gratuitous ARP不同于一般 ...

  2. vue-video-player的使用总结

    由于公司的项目是网上教育的,像网易云课堂那种教育网站,因而会有很多课程需要在线观看的,所以视频插件是必不可少的. 由于我用vue开发项目,所以找视频插件也找和vue贴近的.最后选择了vue-video ...

  3. Java 操纵XML之创建XML文件

    Java 操纵XML之创建XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...

  4. poj 3368 rmq ***

    题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. #include<cstdio> #include<iostream> #incl ...

  5. ftp通用类1

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  6. 前端构建和模块化工具-coolie

    [前言] 假设你之前用过前端模块化工具:seajs.requirejs. 用过前端构建工具grunt.gulp, 而且感到了一些不方便和痛苦,那么你能够试试coolie [coolie] 本文不是一篇 ...

  7. 搭建基于crtmpserver的点播解决方案

    1. linux环境下build并启动crtmpserver 这部分可以参见我写的专项详解文章 <crtmpserver流媒体服务器的介绍与搭建> 和 <crtmpserver配置文 ...

  8. .NET轻量级ORM组件Dapper修炼手册

    一.摘要 1.1.为什么叫本次的分享课叫<修炼手册>? 阿笨希望本次的分享课中涉及覆盖的一些小技巧.小技能给您带来一些帮助.希望您在日后工作中把它作为一本实际技能手册进行储备,以备不时之需 ...

  9. 【GitLab】gitlab上配置webhook后,点击测试报错:Requests to the local network are not allowed

    gitlab上配置webhook后,点击测试报错: Requests to the local network are not allowed 操作如下: 报错: 错误原因: gitlab 10.6 ...

  10. Cocos2d-x开源、跨平台的游戏引擎

    from://http://blog.linguofeng.com/pages/language/c/Cocos2dx.html Cocos2d-x 开源.跨平台的游戏引擎 一.下载 http://c ...