Jsoncpp学习笔记(一)

源码网址

转:简单运用

[class] Json::Reader

    [public]
    [将字符串或者输入流转换为JSON的Value对象]
        bool parse( const std::string &document, Value &root, bool collectComments = true );
        bool parse( const char *beginDoc, const char *endDoc, Value &root,bool collectComments = true );        
        bool parse( std::istream &is,Value &root,bool collectComments = true ); // 从文件流中读取
        // ture success ; false error
        [获取满足相应条件的Value]
        std::string getFormatedErrorMessages() const;//返回成员命名的关键如果它存在,否则defaultValue


[class] Json::Value

[注意] Json::Value 只能处理 ANSI 类型的字符串,如果 C++ 程序是用 Unicode 编码的,最好加一个 Adapt 类来适配
[注意] 要取下标为 0 的value值, 只能通过 int i=0;value[i]; 不能是 value[0];
    [public]
    [获取满足相应条件的Value]
        Value get( Uint index, const Value &defaultValue ) const;
        Value get( const char *key, const Value &defaultValue) const;   //查找是否存在 索引key ,存在则返回其对应的 :value
        Value get( const std::string &key, const Value &defaultValue ) const;
        例如:
            Json::root["one"] = 456;
            Json::Value root04 = root.get("one", root);
            cout << root04.asUInt() << endl; //456
    [Value转基本格式]
        Int asInt() const;
        UInt asUInt() const;  //能不转有符号的int
        Int64 asInt64() const;
        LargestInt asLargesInt() const;
        LargestUInt asLargestUInt() const;
        float asFloat() const;
        double asDouble() const;
        bool asBool() const;
        std::string asString() const;
        const char* asCString() const;
        std::string toStyledString() const;  // 可以把整个 value 转为 string 格式
    [判断Value格式]
        bool isNull() const;
        bool isBool() const;
        bool isDouble() const;
        bool isInt() const;
        bool isString() const;
        bool isArrar() const;
        bool isObject() const;
        bool isMember (const char *key) const  //Return true if the object has a member named key.
        bool isMember (const std::string &key) const  //Return true if the object has a member named key.
     [相同类型的比较、交换、类型的获取]
        int compare( const Value &other );  //比较两个 Value
        void swap(Value &other);  //交换两个 Value 的内容
        ValueType type()const;    //Value 的格式
    [其他功能]
        ArrayIndex size() const;  //Number of values in array or object  //typedef unsigned int Json::ArrayIndex        
        Value& append(const Value &value) //append value to < array > at the end;
        void clear();   //remove all object members and array elements
        void resize(ArrayIndex index); //调整array元素的个数,其他格式无法使用
        void empty() const;   //Number of values in array or object
        Value removeMember(const char* key); //Remove and return the named member
        Value removeMember(const std::string &key); //Same as removeMember(const char*).
    [勾造函数]
         Value (ValueType type=nullValue) Create a default Value of the given type.
        Value (Int value)
        Value (UInt value)
        Value (Int64 value)
        Value (UInt64 value)
        Value (double value)
        Value (const char *value)
        Value (const char *beginValue, const char *endValue)
        Value (const StaticString &value) Constructs a value from a static string.         
        Value (const std::string &value)
        Value (bool value)
        Value (const Value &other)    
    [迭代器] Json::Value::const_iterator
        const_iterator     begin () const
        const_iterator     end () const
        iterator     begin ()
        iterator     end ()
        //这个迭代器不要使用 != == 进行比较
    [操作符比较]
        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 //类型不相等返回 0 ,内容一致返回 1
            例: char arr[4] = {'1','2','3','\0'}; string str = "123";
                root["one"] == "123";
                root["one"] == str;
                root["one"] == arr;
                root["one"].asString() == arr;
                root["one"].asString() == str;
                root["one"].asString().c_str() == str.c_str();  //error                
        bool     operator!= (const Value &other) const //类型不相等返回 1 ,内容一致返回 0
        比较类型,类型相等->比较元素个数,个数相等->比较元素值
(2) 数组访问
Json::Value //格式如下
[["key1":value1],["key2":value2]]

Json::Value::const_iterator iter;  //迭代器
for(iter = input.begin(); iter != input.end(); iter++)
Json::Value::Members member=(*iter).getMemberNames();
*(member.begin());          // 输出 key1,key2
(*iter)[*(member.begin())];     //输出 value1,value2
    

[class] Json::FastWriter

    [public]
        void     enableYAMLCompatibility ()                  //转格式时,是否在 :后面加一空格
        virtual std::string     write (const Value &root) //把 Value 转为 std::string,不格式化
        

[class] Json::StyledWriter

是格式化后的json

不支持utf-8格式的输出,需要自己调用writer之后,用iconv转化成utf-8字符串  //见 iconv 文件

[class] Json::Writer 

继承 FastWriter  StyledWriter, 它是一个虚类



[type]

   enum ValueType
   {
      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).
   };

你可能感兴趣的:(c++)