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

有什么网站可以做编程题今日热榜官网

有什么网站可以做编程题,今日热榜官网,seo资源,怎样做美瞳代购网站1. json库 我个人比较喜欢 nlohmann/json.hpp 这个库&#xff0c;因为它只需要一个hpp文件即可&#xff0c;足够轻量&#xff01; 这是它的github地址。 2. 简单实例代码 #include <iostream> #include <json.hpp> #include <fstream> #include <stri…

1. json库

我个人比较喜欢 nlohmann/json.hpp 这个库,因为它只需要一个hpp文件即可,足够轻量!

这是它的github地址。

2. 简单实例代码

#include <iostream>
#include <json.hpp>
#include <fstream>
#include <string>using namespace nlohmann;
using namespace std;string GenerateJsonStr()
{/** 生成json格式字符串** 预计的json格式文本为:* {*   "name": "ashui",*   "school": ["A school", "B school", "C school"],*   "money": [124.5, 345.3, 457.4],**   physic*   {*       "height": 180,*       "weight": 68.5*   }* }*/json json_data;json_data["name"] = "ashui";json_data["school"] = { "A school", "B school", "C school" };json_data["money"] = { 124.5, 345.3, 457.4 };json physic_data;physic_data["height"] = 180;physic_data["weight"] = 68.5;json_data["physic"] = physic_data;return json_data.dump();
}bool SaveJsonStr()
{string jsonStr = GenerateJsonStr();// Save to filestd::ofstream file("output.json");if (file.is_open()){file << jsonStr;file.close();std::cout << "dump string:" << std::endl;std::cout << jsonStr << std::endl;return true;}return false;
}bool ReadJsonObj()
{std:ifstream jsonFile("D:\\Project\\C++\\json_example\\x64\\Debug\\output.json");if (!jsonFile.is_open()){std::cout << "Open json file error" << std::endl;return false;}json jsonData;try{jsonData = json::parse(jsonFile);if (jsonData.is_null()){std::cout << "The json object is null" << std::endl;return false;}std::string name = jsonData["name"];std::vector<std::string> school = jsonData["school"];std::vector<float> money = jsonData["money"];int height = jsonData["physic"]["height"];float weigjt = jsonData["physic"]["weight"];std::cout << std::endl;std::cout << "parse string:" << std::endl;std::cout << "name: " << name << std::endl;std::cout << "height: " << height << std::endl;std::cout << "weigjt: " << weigjt << std::endl;std::cout << "school: ";for (auto s : school){std::cout << s << ", ";}std::cout << std::endl;std::cout << "money: ";for (auto m : money){std::cout << m << ", ";;}std::cout << std::endl;}catch (const std::exception& e){std::cout << "json error: "<< e.what() << std::endl;return false;}return true;
}int main()
{SaveJsonStr();ReadJsonObj();
}

值得注意的是,在使用[]读取json内容时,最外层一定要套一个try-catch用以补获异常,这样比起一个一个元素去判断是否为空、是否为数组等要方便且安全!

http://www.tj-hxxt.cn/news/94054.html

相关文章:

  • 美国做i网站百度搜索引擎网址格式
  • 赤壁专业建站公司网站快速排名推荐
  • wap手机网站源码软文世界
  • 网站怎么做隐藏内容上海seo网站排名优化公司
  • 一元购网站建设多少钱百度地图导航2022最新版
  • 室内设计网站源码下载网络营销推广外包平台
  • 长沙做网站一般多少钱合适短视频seo营销系统
  • 雅安做网站外贸商城建站
  • 成都做网站做的好的公司网站制作app
  • seo教程排名第一北京网站优化方案
  • 网站设计公司如何盈利临沂seo代理商
  • 湖北省利川市建设局网站谷歌推广怎么样
  • 阜新市网站建设网站域名购买
  • 地方网站需要什么手续百度seo怎么收费
  • 网站管理的主要工作有哪些新闻头条最新消息今天发布
  • 淘宝客网站开源汕头网站排名优化
  • 企业信息管理系统的组成不包括seo咨询推广找推推蛙
  • 泉州专业做网站公司电脑培训班在哪里有最近的
  • 长沙网站建设开发成品网站建站空间
  • 网站建设 保密广州抖音seo公司
  • 网站后台文档google关键词工具
  • 做设计在哪个网站投递简历策划公司
  • 石家庄网络开发公司兰州网络推广优化怎样
  • 哪里有好的免费的网站建设淘数据
  • 小白如何做网站短视频seo代理
  • 零基础学做网站适合员工的培训课程
  • 做动态网站需要什么书网站维护收费标准
  • 哪个网站做供求信息长春疫情最新情况
  • 网站页面权重收录批量查询
  • 海口cms建站系统怎样推广自己的网站