云备份项目->jsoncpp

认识 json
json 是一种数据交换格式,采用完全独立于编程语言的文本格式来存储和表示数据。
例如:小明同学的学生信息
char name = " 小明 " ;
int age = 18 ;
float score [ 3 ] = { 88.5 , 99 , 58 };
json 这种数据交换格式是将这多种数据对象组织成为一个字符串:
[
  {
        " 姓名 " : " 小明 " ,
        " 年龄 " : 18 ,
        " 成绩 " : [ 88.5 , 99 , 58 ]
  },
  {
        " 姓名 " : " 小黑 " ,
        " 年龄 " : 18 ,
        " 成绩 " : [ 88.5 , 99 , 58 ]
  }
]
json 数据类型:对象,数组,字符串,数字
对象:使用花括号 {} 括起来的表示一个对象。
数组:使用中括号 [] 括起来的表示一个数组。
字符串:使用常规双引号 "" 括起来的表示一个字符串
数字:包括整形和浮点型,直接使用。
认识  jsoncpp
jsoncpp 库用于实现 json 格式的序列化和反序列化,完成将多个数据对象组织成为 json 格式字符串,以及将 json 格式字符串解析得到多个数据对象的功能。
这其中主要借助 三个类 以及其对应的少量成员函数完成:
//Json 数据对象类
class Json::Value {
    Value & operator = ( const Value & other ); //Value 重载了 [] = ,因此所有的赋值和获取数据都可以通过
    Value & operator []( const std::string & key ); // 简单的方式完成 val[" 姓名 "] = " 小明 ";
    Value & operator []( const char* key );
    Value removeMember ( const char* key ); // 移除元素
    const Value & operator []( ArrayIndex index ) const ; //val[" 成绩 "][0]
    Value & append ( const Value & value ); // 添加数组元素 val[" 成绩 "].append(88);
    ArrayIndex size () const ; // 获取数组元素个数 val[" 成绩 "].size();
    std::string asString () const ; // string string name = val["name"].asString();
    const char* asCString () const ; // char*   char *name = val["name"].asCString();
    Int asInt () const ; // int int age = val["age"].asInt();
    float asFloat () const ; // float
    bool asBool () const ; // bool
};
//json 序列化类,低版本用这个更简单
class JSON_API Writer {
  virtual std::string write ( const Value & root ) = 0 ;
}
class JSON_API FastWriter : public Writer {
  virtual std::string write ( const Value & root );
}
class JSON_API StyledWriter : public Writer {
  virtual std::string write ( const Value & root );
}
//json 序列化类,高版本推荐,如果用低版本的接口可能会有警告
class JSON_API StreamWriter {
    virtual int write ( Value const & root , std::ostream * sout ) = 0 ;
}
class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
    virtual StreamWriter * newStreamWriter () const ;
}
//json 反序列化类,低版本用起来更简单
class JSON_API Reader {
bool parse ( const std::string & document , Value & root , bool collectComments = true );
}
//json 反序列化类,高版本更推荐
class JSON_API CharReader {
    virtual bool parse ( char const * beginDoc , char const * endDoc ,
                      Value * root , std::string * errs ) = 0 ;
}
class JSON_API CharReaderBuilder : public CharReader::Factory {
    virtual CharReader * newCharReader () const ;
}
jsoncpp  实现序列化
#include <iostream>
#include <sstream>
#include <string>
#include <memory>
#include <jsoncpp/json/json.h>

int main()
{
    const char *name="小明";
    int age=18;
    float score[]={77.5,88,93.6};

    Json::Value root;
    root["姓名"]=name;
    root["年龄"]=age;
    root["成绩"].append(score[0]);
    root["成绩"].append(score[1]);
    root["成绩"].append(score[2]);

    Json::StreamWriterBuilder swb;
    std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
    std::stringstream ss;
    sw->write(root,&ss);
    std::cout<<ss.str()<<std::endl;
    return 0;
}

运行结果:

jsoncpp  实现反序列化
#include <iostream>
#include <string>
#include <memory>
#include <jsoncpp/json/json.h>

int main()
{
    std::string str=R"({"姓名":"小黑","年龄":19,"成绩":[58.5,56,59]})";
    Json::Value root;
    Json::CharReaderBuilder crb;
    std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
    std::string err;
    bool ret=cr->parse(str.c_str(),str.c_str()+str.size(),&root,&err);
    if(ret==false)
    {
        std::cout<<"parse error: "<<err<<std::endl;
        return -1;
    }
    std::cout<<root["姓名"].asString()<<std::endl;
    std::cout<<root["年龄"].asInt()<<std::endl;
    int sz=root["成绩"].size();
    for(int i=0;i<sz;i++)
    {
        std::cout<<root["成绩"][i]<<std::endl;
    }
    return 0;
}

运行结果:

  • 33
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新绿.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值