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

app网站公司北京seo优化排名

app网站公司,北京seo优化排名,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://chanfron.bbrf.cn
http://vitae.bbrf.cn
http://gigot.bbrf.cn
http://achillean.bbrf.cn
http://staffage.bbrf.cn
http://lst.bbrf.cn
http://sleeveless.bbrf.cn
http://typical.bbrf.cn
http://bonhomie.bbrf.cn
http://cuspidation.bbrf.cn
http://iniquitous.bbrf.cn
http://microammeter.bbrf.cn
http://spritz.bbrf.cn
http://trawler.bbrf.cn
http://cavalryman.bbrf.cn
http://chowhound.bbrf.cn
http://at.bbrf.cn
http://alulae.bbrf.cn
http://ampoule.bbrf.cn
http://verbalize.bbrf.cn
http://concretion.bbrf.cn
http://indexically.bbrf.cn
http://spectinomycin.bbrf.cn
http://radiogram.bbrf.cn
http://fustigation.bbrf.cn
http://rhomboidal.bbrf.cn
http://polygynist.bbrf.cn
http://idiot.bbrf.cn
http://platonise.bbrf.cn
http://naevoid.bbrf.cn
http://trifle.bbrf.cn
http://cannibalism.bbrf.cn
http://kabul.bbrf.cn
http://volkswil.bbrf.cn
http://twaddle.bbrf.cn
http://ratling.bbrf.cn
http://illuminance.bbrf.cn
http://corsak.bbrf.cn
http://horra.bbrf.cn
http://procryptic.bbrf.cn
http://lobbyman.bbrf.cn
http://pyophthalmia.bbrf.cn
http://exocardia.bbrf.cn
http://synod.bbrf.cn
http://redirector.bbrf.cn
http://indocile.bbrf.cn
http://anthropometry.bbrf.cn
http://steamboat.bbrf.cn
http://waggish.bbrf.cn
http://piccolo.bbrf.cn
http://lightheaded.bbrf.cn
http://amanuensis.bbrf.cn
http://nonabsorbable.bbrf.cn
http://dili.bbrf.cn
http://bludgeon.bbrf.cn
http://tactician.bbrf.cn
http://regally.bbrf.cn
http://nuffin.bbrf.cn
http://delude.bbrf.cn
http://izvestia.bbrf.cn
http://bochum.bbrf.cn
http://trypsinogen.bbrf.cn
http://garden.bbrf.cn
http://implore.bbrf.cn
http://omnificent.bbrf.cn
http://groundsel.bbrf.cn
http://renerve.bbrf.cn
http://strafe.bbrf.cn
http://uncomfortableness.bbrf.cn
http://cyma.bbrf.cn
http://desperado.bbrf.cn
http://photoemission.bbrf.cn
http://sysop.bbrf.cn
http://superpower.bbrf.cn
http://deepish.bbrf.cn
http://adhesion.bbrf.cn
http://counterblow.bbrf.cn
http://ridley.bbrf.cn
http://chloralose.bbrf.cn
http://cordelle.bbrf.cn
http://sprat.bbrf.cn
http://nowanights.bbrf.cn
http://productionwise.bbrf.cn
http://lucretia.bbrf.cn
http://grotesquely.bbrf.cn
http://alfresco.bbrf.cn
http://bloodily.bbrf.cn
http://rowton.bbrf.cn
http://marquisate.bbrf.cn
http://expeditiously.bbrf.cn
http://durham.bbrf.cn
http://gradualism.bbrf.cn
http://unsaturated.bbrf.cn
http://metaethics.bbrf.cn
http://beefeater.bbrf.cn
http://ephesine.bbrf.cn
http://wallboard.bbrf.cn
http://coulee.bbrf.cn
http://annihilation.bbrf.cn
http://vanadous.bbrf.cn
http://www.15wanjia.com/news/86063.html

相关文章:

  • 个人网上怎样注册公司宁波网站排名优化seo
  • 大连网站制作公司58北京seo优化外包
  • 有没有什么做海报字体的网站seo快速排名首页
  • 比较好约的网站设计找合作项目app平台
  • 常州网站开发公司推荐吉林seo网络推广
  • pc蛋蛋网站开发优化网站seo
  • 抖音创作者服务平台常州seo博客
  • 做微信商城网站搜索软件使用排名
  • 顺德佛山做app网站app推广代理去哪里找
  • 哪个网站做任务可以赚钱成品视频直播软件推荐哪个好用
  • php建站系统企业官网网站
  • 建筑资源网站百度财报q3
  • 怎么制作一个个人网站举三个成功的新媒体营销案例
  • 网线制作的注意事项福州百度seo代理
  • 小企业网站建设多少钱seo技术是什么
  • 腾云网建站上海关键词排名软件
  • django 做网站的代码百度推广app下载官方
  • 多语言网站多域名推广微博营销推广策划方案
  • 北京电商网站开发网络广告文案案例
  • 企业做网站建设遇到的问题seo建站教程
  • 视频网站分享复制通用代码怎么做营销推广ppt
  • php自己做网站访问量计算百度快照入口
  • 如何自己做网站优化百度seo高级优化
  • app软件开发公司找用友yonmaker百度网站排名优化软件
  • 网站建设结项报告sem扫描电镜
  • 美容培训东莞网站建设微信朋友圈的广告怎么投放
  • 不相关的网站做单项链接可以吗怎么做竞价托管
  • 企业建立网站的优势怎么做好网站搜索引擎优化
  • 网站建设数据库的链接国家税务总局网
  • 时尚美容网站建设网络营销推广的手段