认识 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 ; // 转 floatbool 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;
}
运行结果: