菜鸟笔记
提升您的技术认知

C++中JSON的使用详解

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

#include 
   
    定义定义
#include 
    
     
#include "json.h"
using namespace std;

//定义jsoncpp 支持的对象类型
enum Type
{
    nullValue = 0, ///< 'null' value
    intValue,      ///< signed integer value
    uintValue,     ///< unsigned integer value
    realValue,     ///< double value
    stringValue,   ///< UTF-8 string value
    booleanValue,  ///< bool value
    arrayValue,    ///< array value (ordered list)
    objectValue    ///< object value (collection of name/value pairs).
};

int main()
{
    Json::Value root;
    root["type"] = nullValue;
    root["name"] = "Xin Ma";
    root["pwd"] = "123456";

    string tmpstr = root.toStyledString();
    
    char buff[1024] = {0};
    strcpy_s(buff, strlen(tmpstr.c_str())+1, tmp.c_str());
    cout<<"buff_root :"<
     
      <