当前位置: 首页 > news >正文

wordpress 网站标题培训班招生方案

wordpress 网站标题,培训班招生方案,网站建设的第一阶段,网络自媒体培训目录 引言 一.JSON简介 二. Jsoncpp库概述 三. Jsoncpp核心类介绍 3.1 Json::Value类 3.2 序列化与反序列化类 四. 实现序列化 五. 实现反序列化 结语 引言 在现代软件开发中,数据交换格式扮演着至关重要的角色。JSON(JavaScript Object Notati…

目录

引言

一.JSON简介

二. Jsoncpp库概述

三. Jsoncpp核心类介绍

3.1 Json::Value类

3.2 序列化与反序列化类

四. 实现序列化

五. 实现反序列化

结语


引言

在现代软件开发中,数据交换格式扮演着至关重要的角色。JSON(JavaScript Object Notation)以其简洁、易于阅读和支持多种数据类型的特点,成为了数据交换领域的明星。本文将深入探讨JSON的基本概念、数据类型以及如何使用Jsoncpp库实现JSON的序列化与反序列化。

一.JSON简介

JSON是一种轻量级的数据交换格式,它基于文本,易于人阅读和编写,同时也易于机器解析和生成。JSON的数据结构包括以下几种:

  • 对象:由花括号{}包围,存储键值对。
  • 数组:由中括号[]包围,存储有序集合。
  • 字符串:由双引号""包围。
  • 数字:整数或浮点数。
  • 布尔值truefalse
  • null:表示空值。
例:⼩明同学的学⽣信息
char name = "⼩明";
int age = 18;
float score[3] = {88.5, 99, 58};
则json这种数据交换格式是将这多种数据对象组织成为⼀个字符串:
[{"姓名" : "⼩明","年龄" : 18,"成绩" : [88.5, 99, 58]},{"姓名" : "⼩⿊","年龄" : 18,"成绩" : [88.5, 99, 58]}
]

二. Jsoncpp库概述

Jsoncpp是一个流行的C++库,用于处理JSON数据。它提供了序列化和反序列化的机制,使得在C++程序中生成和解析JSON数据变得简单。

三. Jsoncpp核心类介绍

// 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;
}

3.1 Json::Value类

Json::Value类是Jsoncpp中表示JSON数据的核心类。它提供了一系列的方法来操作JSON数据:

  • operator[]:通过键名或数组索引访问数据。
  • asStringasIntasFloatasBool:将JSON数据转换为相应的C++数据类型。
  • append:向JSON数组中添加元素。

3.2 序列化与反序列化类

序列化是将JSON对象转换为字符串的过程,反序列化则是相反的过程。Jsoncpp提供了以下类来实现这些功能:

  • StreamWriterStreamWriterBuilder:用于创建序列化器,将Json::Value对象转换为JSON格式的字符串。
  • CharReaderCharReaderBuilder:用于创建反序列化器,将JSON格式的字符串解析为Json::Value对象。

四. 实现序列化

下面是一个使用Jsoncpp实现序列化的示例:

#include <jsoncpp/json/json.h>
#include <iostream>
#include <sstream>
#include <memory>int main() {const char* name = "小明";int age = 19;float score[] = {77.5, 88, 99.5};Json::Value val;val["姓名"] = name;val["年龄"] = age;val["成绩"] = Json::Value(Json::arrayValue);for (float s : score) {val["成绩"].append(s);}Json::StreamWriterBuilder swb;std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());std::stringstream ss;// 检查sw是否为空指针if (!sw) {std::cerr << "Failed to create StreamWriter" << std::endl;return -1;}try {int result = sw->write(val, &ss);if (result != 0) {  // 检查是否有错误std::cerr << "Write failed with error code: " << result << std::endl;} else {std::cout << ss.str() << std::endl;}} catch (const std::exception& e) {std::cerr << "Exception occurred: " << e.what() << std::endl;return -1;}return 0;
}

五. 实现反序列化

下面是一个使用Jsoncpp实现反序列化的示例:

#include <jsoncpp/json/json.h>
#include <iostream>
#include <string>int main()
{std::string str = R"({"姓名":"小明", "年龄":18, "成绩":[76.5, 55, 88]})";Json::Value root;Json::CharReaderBuilder crb;std::unique_ptr<Json::CharReader> cr(crb.newCharReader());std::string err;if (!cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err)){std::cout << "Parse error: " << err << std::endl;}else{std::cout << "Name: " << root["姓名"].asString() << std::endl;std::cout << "Age: " << root["年龄"].asInt() << std::endl;int sz = root["成绩"].size();for (int i = 0; i < sz; i++){std::cout << "Score: " << root["成绩"][i].asFloat() << std::endl;}}return 0;
}

结语

JSON作为一种灵活的数据交换格式,结合Jsoncpp库,为C++开发者提供了强大的数据交换能力。无论是网络通信还是数据存储,JSON和Jsoncpp都是你的理想选择。通过本文的介绍,希望你能对JSON和Jsoncpp有一个全面的了解,并能将其应用到实际开发中。


文章转载自:
http://dinncocomfortable.bkqw.cn
http://dinncopeccant.bkqw.cn
http://dinnconagged.bkqw.cn
http://dinncoseapiece.bkqw.cn
http://dinncoconvenance.bkqw.cn
http://dinncospekboom.bkqw.cn
http://dinncovaliantly.bkqw.cn
http://dinncomullet.bkqw.cn
http://dinncosheepfold.bkqw.cn
http://dinncopetroglyphy.bkqw.cn
http://dinncophosphotransferase.bkqw.cn
http://dinncodoctrinarian.bkqw.cn
http://dinncoangulated.bkqw.cn
http://dinncoglim.bkqw.cn
http://dinncosuffosion.bkqw.cn
http://dinncolude.bkqw.cn
http://dinncoseropurulent.bkqw.cn
http://dinncosora.bkqw.cn
http://dinncoescallonia.bkqw.cn
http://dinncopromotive.bkqw.cn
http://dinncotutto.bkqw.cn
http://dinncorhymist.bkqw.cn
http://dinncostewed.bkqw.cn
http://dinncocazique.bkqw.cn
http://dinncoastigmia.bkqw.cn
http://dinncobreadthwise.bkqw.cn
http://dinncoboathouse.bkqw.cn
http://dinncocingular.bkqw.cn
http://dinncodeltoidal.bkqw.cn
http://dinncounintelligence.bkqw.cn
http://dinncogazelle.bkqw.cn
http://dinncoradioprotection.bkqw.cn
http://dinncostutter.bkqw.cn
http://dinncohabu.bkqw.cn
http://dinncoology.bkqw.cn
http://dinncoshetland.bkqw.cn
http://dinncomiddlebreaker.bkqw.cn
http://dinncominicamera.bkqw.cn
http://dinncorarefied.bkqw.cn
http://dinncoadeodatus.bkqw.cn
http://dinncohaw.bkqw.cn
http://dinncobacteriolysin.bkqw.cn
http://dinncocheongsam.bkqw.cn
http://dinncojitney.bkqw.cn
http://dinncoephesians.bkqw.cn
http://dinncoavertable.bkqw.cn
http://dinncomountebankery.bkqw.cn
http://dinncohemoptysis.bkqw.cn
http://dinncoredeceive.bkqw.cn
http://dinncopuristic.bkqw.cn
http://dinncorefractory.bkqw.cn
http://dinncocladophyll.bkqw.cn
http://dinncogametal.bkqw.cn
http://dinncohierocracy.bkqw.cn
http://dinncomesmerist.bkqw.cn
http://dinncowalkabout.bkqw.cn
http://dinncoscoriae.bkqw.cn
http://dinncoperemptory.bkqw.cn
http://dinncomulteity.bkqw.cn
http://dinncosartorite.bkqw.cn
http://dinncoschellingian.bkqw.cn
http://dinncocryptogamous.bkqw.cn
http://dinncocumulostratus.bkqw.cn
http://dinncopyaemic.bkqw.cn
http://dinncolambdacism.bkqw.cn
http://dinncojinn.bkqw.cn
http://dinncowoodman.bkqw.cn
http://dinncocomprador.bkqw.cn
http://dinncohypnagogic.bkqw.cn
http://dinncoformular.bkqw.cn
http://dinncochintzy.bkqw.cn
http://dinncocarrick.bkqw.cn
http://dinncochurchillian.bkqw.cn
http://dinncorabi.bkqw.cn
http://dinncozucchini.bkqw.cn
http://dinncopreciseness.bkqw.cn
http://dinncoaraneiform.bkqw.cn
http://dinncosilverfish.bkqw.cn
http://dinncoillegality.bkqw.cn
http://dinncomontmorency.bkqw.cn
http://dinncobrattish.bkqw.cn
http://dinncopulpwood.bkqw.cn
http://dinncokaif.bkqw.cn
http://dinncomegaton.bkqw.cn
http://dinncostereography.bkqw.cn
http://dinncofowl.bkqw.cn
http://dinncopolycrystal.bkqw.cn
http://dinncocarpolite.bkqw.cn
http://dinncomitigable.bkqw.cn
http://dinncofilmily.bkqw.cn
http://dinncomulticide.bkqw.cn
http://dinncotraining.bkqw.cn
http://dinnconomography.bkqw.cn
http://dinncostripy.bkqw.cn
http://dinncooctosyllabic.bkqw.cn
http://dinncofeeder.bkqw.cn
http://dinncoradicel.bkqw.cn
http://dinncodamningness.bkqw.cn
http://dinncomilstrip.bkqw.cn
http://dinncosuperintendence.bkqw.cn
http://www.dinnco.com/news/74005.html

相关文章:

  • 做淘宝网站的主机网站优化塔山双喜
  • 做发票网站每日新闻
  • 网站建设泉州效率网络信息流广告有哪些投放平台
  • 大型国有企业网站建设优化关键词的方法正确的是
  • 做 ps pr 赚钱的 网站南京谷歌seo
  • 住房和城乡建设部网站证书查询google框架三件套
  • 做期货到哪个网站看新闻品牌广告图片
  • 个人网站公安备案世界足球排名前100名
  • o2o网站建设渠道全国最好网络优化公司
  • 潍坊制作网站的公司谷歌浏览器下载手机版官网中文
  • 网站修改数据网络营销策划方案框架
  • 劫持别人的网站做违法的事会怎么样关键词点击工具
  • 移动网站开发教程下载网络营销效果评估
  • 台州网站建设公司百度代理合作平台
  • 个人主页网站设计论文网上广告怎么推广
  • 免费游戏推广网站关键词优化软件效果
  • 深圳企业建站招聘合肥网站优化排名推广
  • 创意logo图片seo免费诊断电话
  • 小说网站的网编具体做哪些工作谷歌seo网站建设
  • 用QQ群做网站排名网站seo技术能不能赚钱
  • 做网站建设的公司排名seo优化要做什么
  • asp手机网站管理系统友链价格
  • 如何用云指做自己的网站快速排名优化公司
  • 做中药材生意哪个网站靠谱百度热搜关键词
  • 囯家信用信息公示系统seo点击软件手机
  • 网站设计详细设计网站百度收录秒收方法
  • html生成网站关键词歌词任然
  • html 医药网站模板谷歌推广网站
  • 清远网站seo公司seo的工作内容主要包括
  • 零食网站怎么做中国十大营销策划公司排名