目录
1.介绍
json
是一种数据交换格式,采用完全独立于编程语言的文本格式来存储和表示数据json
数据类型:对象、数组、字符串、数字
- 对象:使用
{}
括起来的表示一个对象 - 数组:使用
[]
括起来的表示一个数组 - 字符串:使用
""
括起来的表示一个字符串 - 数字:包括整形和浮点型,直接使用
2.jsoncpp
jsoncpp
库用于实现json
格式的序列化和反序列化,完成将多个数据对象组织成为格式字符串解析得到多个数据对象的功能- 主要借助三个类以及其对应的少量成员函数完成:
| |
| class Json::Value |
| { |
| |
| Value &operator=(const Value &other); |
| |
| |
| Value& operator[](const std::string& key); |
| Value& operator[](const char* key); |
| |
| |
| Value removeMember(const char* key); |
| |
| |
| const Value& operator[](ArrayIndex index) const; |
| |
| |
| Value& append(const Value& value); |
| |
| |
| ArrayIndex size() const; |
| |
| |
| std::string asString() const; |
| |
| |
| const char* asCString() const; |
| |
| |
| int asInt() const; |
| |
| |
| float asFloat() const; |
| |
| |
| bool asBool() const; |
| }; |
| |
| |
| 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); |
| } |
| |
| |
| 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; |
| } |
| |
| |
| class JSON_API Reader |
| { |
| bool parse(const std::string& document, |
| Value& root, bool collectComments = true); |
| } |
| |
| |
| 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; |
| } |
复制
3.使用
1.main.cc
| int main() |
| { |
| char name[] = "SnowK"; |
| int age = 18; |
| float score[3] = {100, 99, 98}; |
| |
| Json::Value stu; |
| stu["Name"] = name; |
| stu["Age"] = age; |
| stu["Score"].append(score[0]); |
| stu["Score"].append(score[1]); |
| stu["Score"].append(score[2]); |
| |
| std::string str; |
| if(Serialize(stu, str) == false) |
| { |
| return -1; |
| } |
| std::cout << str << std::endl; |
| std::cout << "-------------------------------" << std::endl; |
| |
| Json::Value val; |
| if(UnSerialize(str, val) == false) |
| { |
| return -1; |
| } |
| |
| std::cout << val["Name"].asString() << std::endl; |
| std::cout << val["Age"].asInt() << std::endl; |
| for (int i = 0; i < val["Score"].size(); i++) |
| { |
| std::cout << val["Score"][i].asInt() << std::endl; |
| } |
| |
| return 0; |
| } |
复制
2.序列化
| bool Serialize(const Json::Value &val, std::string &dest) |
| { |
| |
| Json::StreamWriterBuilder swb; |
| std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter()); |
| |
| |
| std::stringstream ss; |
| if (sw->write(val, &ss) != 0) |
| { |
| std::cout << "Json序列化失败" << std::endl; |
| return false; |
| } |
| |
| dest = ss.str(); |
| |
| return true; |
| } |
复制
3.反序列化
| bool UnSerialize(const std::string &src, Json::Value &val) |
| { |
| Json::CharReaderBuilder crb; |
| std::unique_ptr<Json::CharReader> cr(crb.newCharReader()); |
| |
| std::string err; |
| if (cr->parse(src.c_str(), src.c_str() + src.size(), &val, &err) == false) |
| { |
| std::cout << "json反序列化失败: " << err << std::endl; |
| return false; |
| } |
| |
| return true; |
| } |
复制