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