C++中JSON的使用詳解__C++

來源:互聯網
上載者:User

    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 :"<

                                 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.