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

做问卷调查用哪个网站免费推广网站入口

做问卷调查用哪个网站,免费推广网站入口,班级网站素材下载,西樵网站建设公司《二、基础方法》:节点访问、值获取、显式 vs 隐式、异常处理、迭代器、类型检测、异常处理……一节课搞定C处理JSON数据85%的需求…… JSON 字段的简单类型包括:number、boolean、string 和 null(即空值);复杂类型则有…

《二、基础方法》:节点访问、值获取、显式 vs 隐式、异常处理、迭代器、类型检测、异常处理……一节课搞定C++处理JSON数据85%的需求……

在这里插入图片描述

JSON 字段的简单类型包括:number、boolean、string 和 null(即空值);复杂类型则有 对象(Object)和数组(Array)两类。

1 节点与值

访问对象的指定名字的某个字段,可以使用 [“key”] ,访问指定下标的某个元素,可以使用 [ index ] 来访问(二者本质都是对 [] 操作符的重载),也可以通过方法 at(key/index) 来访问。

当指定名字(key)或下标(index)并无对应数据可供访问时,在 nlohmann/json 中,都被视为越界操作;此时,使用 [ ] 将“喜提”未定义(UB)的结果,而使用 at () 则得到异常。

通过 [] 或 at() 得到是 JSON 节点 (一个字段,或一个数组的一个元素);更多的时候,我们想要得到既定类型的值。

假设有这样一个 nlohmann/json 对象:

nlohmann::json o = 
{{ "id","ORD20250409-191" },{ "customerID", 10345 },{ "items", {123, 94320, 8} },{ "totalAmount", 172.8 },{ "orderDate","2025/04/09" }
};

存在隐式和显式两种取值方法,如下:

int id1 = o["customerID"];  // 隐式
int id2;
id2 = o["customerID"]; // 隐式int id3 = o["customerID"].get<int>(); // 显式,适用定义新变量int id4;
o["customerID"].get_to(id4); // 显式,类型信息来自已定义的 id4

这里的显式或隐式,指的类型转换过程:JSON 节点类型到目标类型的转换过程。隐式转换会在以下两点都满足时,出现问题(造成编译失败):

  1. 目标类型重载了赋值操作符(即: = );
  2. 转换时,目标对象是已定义变量(即:确实在为某个“老”对象赋值,而非在构造新对象)。

如需进一步了解隐式转换出错的原因,建议到 d2school.com 对应课堂阅读扩展内容。

nlohmann/json官方推荐使用 get() 或 get_to (…) 显式指定要转换的目标类型。如果需要的是更严格项目管理,可以在项目中定义全局宏:JSON_USE_IMPLICIT_CONVERSIONS=0以禁用隐式取值,如是CMake项目,在CMakeList.txt 内添加代码: SET (JSON_ImplicitConversions OFF),可得相同效果。

2 迭代器

借助迭代器,有四种 for 循环可用以迭代 JSON 对象的内容(假设 o 为 某json对象):

  • 循环1
for (auto it = o.begin(); it != o.end(); ++it)
{cout << it.key() << ":" << it.value() << "\n";
}
  • 循环2
for (auto it : o.items()) // 本质同循环1
{cout << it.key() << ":" << it.value() << "\n";
}
  • 循环3
for (auto v : o)  // 此时只有 value,因此比较适合遍历数组节点
{cout << v << "\n";
}
  • 循环4
for (auto & [k, v] : o.items()) // 需 c++17 结构化绑定支持
{cout << k << ":" << v << "\n";
}

3 异常

nlohmann/json 日常操作中,有三种常见异常类型(它们的基类都是 nlohmann::json::exception)。

  • json::parse_error / 解析出错

解析的数据中,存在格式(包括编码)非法的数据,典型如:包含了非 UNICODE 编码的汉字内容。nlohmann/json 支持的UNICODE编码具体包括:UTF-8、UTF-16、UTF-32等。

注:“注释”在 JSON 标准规范中,也是一种非常格式,但因为太常见,所以 nlohmann/json 提供了支持(非默认),详见视频一(快速认识)。

  • json::out_of_range / 越界访问

使用 at(key/index) 访问数据时,查找无指定字段名或下标对应的数据时,即抛出该异常。

  • json::type_error / 类型不匹配

典型如,对一个非对象、非数组类型的JSON节点,执行 push_back(新元素) 操作。

4 视频2:基础方法

010-nlohmann/json-2-基础方法

5 示例项目-常用方法

  • 报文 demo.json
{"name" : "丁小明","age" : 12
}
  • 代码
#include <cassert>#include <iostream>
#include <fstream>
#include <string>
#include <vector>#include <nlohmann/json.hpp>using json = nlohmann::json;int main()
{nlohmann::json o1 = {{ "id","ORD20250409-191" },{ "customerID", 10345 },{ "items", {123, 94320, 8} },{ "totalAmount", 172.8 },{ "orderDate","2025/04/09" }};std::cout << o1["id"] << std::endl;std::cout << o1["customerID"] << std::endl;std::cout << o1["items"] << std::endl;std::cout << o1["totalAmount"] << std::endl;std::cout << o1["orderDate"] << std::endl;auto node = o1["id"];std::cout << "node type-name is :\n" << typeid(node).name() << std::endl;// 隐式转换类型,以获取值{std::string id1 = o1["id"];int customerID = o1["customerID"];std::cout << id1 << "," << customerID << std::endl;}// 显式转换类型,以获取值{auto id2 = o1["id"].get<std::string>();auto customerID2 = o1["customerID"].get<int>();std::cout << id2 << "," << customerID2 << std::endl;}{double totalAmount;o1["totalAmount"].get_to(totalAmount);std::cout << totalAmount << std::endl;std::cout << o1["totalAmount"].get_to(totalAmount) << std::endl;        }// find、at {json o; o["name"] = "丁小明";o["age"] = 12;try{std::cout << o["Name"].get<std::string>() << " is "<< o["age"].get<int>() <<std::endl;}catch(std::exception const& e){std::cout << e.what() << std::endl;}auto it = o.find("Name1");if (it != o.end()){std::cout << it->get<std::string>() << std::endl;}else{std::cerr << "no found field : Name1." << std::endl;}try{std::cout << o.at("NAME").get<std::string>() << " is "<< o["age"].get<int>() <<std::endl;}catch(std::exception const& e){std::cout << e.what() << std::endl;}        std::cout << o.dump(2) << std::endl;}// 迭代器、循环{for (auto const it : o1.items()){            std::cout << it.key() << " ==> " << it.value() << "\ttype : " << it.value().type_name() << std::endl;}std::cout << "==================\n";for (auto [k, v] : o1.items()){            std::cout << k << " ==> " << v << "\ttype : " << v.type_name() << std::endl;}o1["items"].push_back(999);std::cout << o1["items"] << std::endl;}// 异常: 非法JSON报文{std::string s = "\"Hello JSON!——第2学堂!\"";try{auto j = json::parse(s);std::cout << j.dump() << std::endl;}catch(json::parse_error const& e){std::cerr << e.id << "->" << e.what() << std::endl;}        }// 从文件读{// 请填写你的 demo.json 的实际位置std::ifstream ifs ("D:\\...\\CommonlyUsedJSON\\demo.json");if (!ifs){std::cerr << "open file fail!" << std::endl;return -1;}try{std::cout << "== read from file : \n";auto j = json::parse(ifs);std::cout << j.dump(2) << std::endl;}catch(json::parse_error const& e){std::cerr << e.what() << std::endl;}}// 异常:尝试和类型不匹配的行为{using namespace nlohmann::literals;json j = R"({"id" : "Hello!","items": [1, 2, 3]})"_json;try{j.at("items").push_back(4);j.at("id").push_back('a');}catch(json::type_error const& e){std::cerr << e.what() << std::endl;}}
}
http://www.dtcms.com/wzjs/117375.html

相关文章:

  • 做的较好的拍卖网站网址缩短
  • 适合做网站服务器的主机百度小说风云榜总榜
  • 网站运营者是做啥工作的网站快速搜索
  • 联雅网站建设营销型企业网站有哪些
  • 免费推广的预期效果seo兼职平台
  • 网站导航作用seo网站培训
  • 山东网站seo设计整合营销策划方案模板
  • 衡水网站建设衡水竞价推广网络推广运营
  • 免费1级做爰网站安卓aso
  • 建站高端网站太原百度快速优化
  • 网站建设辶首选金手指十五宁波关键词优化时间
  • 旅游网站建设背景长沙网站se0推广优化公司
  • 网站备案 超链接114网址大全
  • 网站备案最快几天如何给公司做网络推广
  • 上海建设工程咨询网站广州线下教学
  • 网站开发联系方式年度关键词
  • 怎么给自己公司做网站网络seo培训
  • 谁做视频网站优化教程
  • 穿着高跟鞋做的网站seo网站优化论文
  • 国家住房和城乡建设网站seo关键词优化经验技巧
  • 做网站手机端如何更新广州公关公司
  • 北京城乡建设网站itmc平台seo优化关键词个数
  • 广州荔湾做网站公司index百度指数
  • 常用的网站类型有哪些类型有哪些类型怎么在百度上发布信息广告
  • 如何更改wordpress登录密码错误seo是什么意思
  • 珠海网站建设 金碟电脑零基础培训班
  • 彩票网站代理app拉新平台
  • 临沂网络网站建设推广软件赚钱违法吗
  • wordpress友情链接设置seo搜索引擎优化薪资水平
  • 常州专业房产网站建设百度电商平台app