码迷,mamicode.com
首页 > Web开发 > 详细

JsonCpp使用

时间:2017-04-07 16:36:59      阅读:1910      评论:0      收藏:0      [点我收藏+]

标签:main   sbo   swa   定义   递归   dwr   let   array   读写   

JsonCpp主要包含三种类型的class:Value Reader Writer。

jsoncpp中所有对象、类名都在namespace json中,包含json.h即可

  • Json::Value   :可以表示所有支持的类型,如:int , double ,string , object, array等
  • Json::Reader :将文件流或字符串创解析到Json::Value中,主要使用parse函数。
  • Json::Writer  :  与JsonReader相反,将Json::Value转换成字符串流等。

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 );
    Value:<是jsoncpp 中最基本、最重要的类,用于表示各种类型的对象,jsoncpp 支持的对象类型可见                 Json::ValueType 枚举值; Value类的对象代表一个JSON值,既可以代表一个文档,也可以代表                 文档中一个值。如同JSON中定义的“值”一样,Value是递归的>


        【相同类型的比较、交换、类型的获取】
         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; 


Jsoncpp 的 Json::Writer 类是一个纯虚类,并不能直接使用。在此我们使用 Json::Writer 的子类:Json::FastWriter(将数据写入一行,没有格式),Json::StyledWriter(按json格式化输出,易于阅读)

 

JsonCpp读写示例代码:

#include <json/json.h>
#include <sstream>
#include <iostream>
#include <fstream>

void read_json()
{
    std::ifstream ifs;
    ifs.open("a.json");
    std::stringstream buffer;
    buffer << ifs.rdbuf();
    ifs.close();

    auto str = buffer.str();

    Json::Reader reader;
    Json::Value value;
    if (reader.parse(str, value)) {
        if (value["name"].isString()) {
            std::string name = value["name"].asString();
            std::cout << name << std::endl;
        }

        std::cout << value["age"].asInt() << std::endl;        //根据Key获取值时最好判断类型,否则解析会中断
        
        const Json::Value arrayObj = value["like"];
        for (unsigned int i = 0; i < arrayObj.size(); i++) {
            if (!arrayObj[i].isMember("book"))
                continue;
            auto out = arrayObj[i]["book"].asString();
            std::cout << out;
        }
    }
}

void write_json()
{
    Json::FastWriter write;
    Json::Value root;

    Json::Value item;
    Json::Value arrayObj;
    item["book"] = "c++";
    item["food"] = "apple";
    item["music"] = "ddx";
    arrayObj.append(item);

    root["name"] = "dsw";
    root["age"]  = 18;
    root["like"] = arrayObj;    //注意:这里不能用append,append功能是将Json::Value添加到数组末尾

    auto str = root.toStyledString();
    std::cout << str << std::endl;

    std::ofstream ofss;
    ofss.open("a.json");
    ofss << str;
    ofss.close();
}

int main()
{ write_json(); read_json(); getchar();
return 0; }

 

JsonCpp使用

标签:main   sbo   swa   定义   递归   dwr   let   array   读写   

原文地址:http://www.cnblogs.com/DswCnblog/p/6678708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!