zoukankan      html  css  js  c++  java
  • 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 );  

     

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


                                    

  • 相关阅读:
    fenby C语言 P32
    fenby C语言 P31 使用数组的指针
    fenby C语言 P30
    fenby C语言 P29
    fenby C语言 P28
    fenby C语言 P27使用指针
    fenby C语言 P25
    fenby C语言 P26
    fenby C语言P24
    Python学习之路:通过socket实现处理多个连接
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/8777637.html
Copyright © 2011-2022 走看看