zoukankan      html  css  js  c++  java
  • c++ jsoncpp

        Json::Value root;
        ...
       
        // 转为格式化字符串,里面加了空格及换行符
        string strJson1 = root.toStyledString();   
     
        // 转为未格式化字符串,无多余空格及换行符
        string strJson2 = Json::FastWriter().write(root);

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

    转载

  • 相关阅读:
    redis-LinkedList
    Jedis(java操作redis数据库技术)
    jquery判断表单内容是否为空
    jQuery单选框的回显
    使用jQuery重置(reset)表单的方法
    BootstrapValidator 解决多属性被同时校验问题
    模态框被遮罩层遮挡
    python 高阶函数
    python 函数式编程
    python 生成器
  • 原文地址:https://www.cnblogs.com/Z2978/p/13376237.html
Copyright © 2011-2022 走看看