Detailed explanation of the use of JSON in C++

    jsoncpp mainly contains three classes: Value, Reader, Writer. Note that Json::Value can only handle ANSI type strings. If the C++ program is encoded in Unicode, it is best to add an Adapt class to adapt.


Json inner classes and methods:

    Reader< is for reading, to be precise, for converting strings to Json::Value objects>

       [Constructor]
        1. Reader();
       [Copy constructor]
        2. Reader( const Features &features );
       [Convert a string or input stream to a JSON Value object]
       [The following is the corresponding overloaded function of 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:< is the most basic and important class in jsoncpp, which is used to represent various types of objects. The object types supported by jsoncpp can be seen in the Json::ValueType enumeration value; an object of the Value class represents a JSON value, which can either represent a A document, which can also represent a value in a document. Like "value" defined in JSON, Value is recursive > 

        【构造函数】
         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 );
        [Copy constructor]
         2. Value( const StaticString &value );
          Value( const std::string &value );
          Value( const Value &other );
        [Comparison, exchange, and type acquisition of the same type]
         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;
        [Convert the Value object to the corresponding type]
         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;
        [Corresponding judgment function]
         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;
        【Clear and expand function】
         7、void clear();
         void resize( UInt size );
        [Get the Value that meets the corresponding conditions]
         8、Value get( UInt index, const Value &defaultValue ) const;
         Value get( const std::string &key,const Value &defaultValue ) const;
         Members getMemberNames() const;
        [Delete Values ​​that meet the corresponding conditions]
         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 :"<

                                 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325715107&siteId=291194637
Recommended