vi json_test.cpp
- #include
- 2 #include
- 3 #include
- 4 using namespace std;
- 5
- 6 void Seralize()
- 7 {
- 8 const char*name="张三";
- 9 int age=18;
- 10 float score[]={88.5,99,45};
- 11
- 12 Json::Value val;
- 13 val["姓名"]=name;
- 14 val["年龄"]=age;
- 15 val["成绩"].append(score[0]);
- 16 val["成绩"].append(score[1]);
- 17 val["成绩"].append(score[2]);
- 18
- 19 Json::Value root;
- 20 root.append(val);
- 21
- 22 Json::FastWriter writer;
- 23 string str=writer.write(root);
- 24 cout<
- 25
- 26 }
- 27
- 28
- 29 void UnSeralize()
- 30 {
- 31 string str=R"({"姓名":"李四","年龄":19,"成绩":[66,87.5,99]})";
- 32 cout<
- 33 Json::Reader reader;
- 34 Json::Value val;
- 35 bool ret= reader.parse(str,val);
- 36 if(ret==false)
- 37 {
- 38 cout<<"json parse error\n";
- 39 return;
- 40 }
- 41
- 42 cout<
"姓名"].asString()< - 43 cout<
"年龄"].asInt()< - 44 if(val["成绩"].isArray())
- 45 {
- 46 int sz=val["成绩"].size();
- 47 for(int i=0;i
- 48 {
- 49 cout<
"成绩"][i].asFloat()< - 50 }
- 51 }
- 52 return;
- 53 }
- 54
- 55
- 56
- 57 int main()
- 58 {
- 59
- 60 Seralize();
- 61 UnSeralize();
- 62 return 0;
- 63
- 64 }
-
vi makefile
- http_sever:http_sever.cpp
- g++ -g -std=c++11 $^ -o $@ -lpthread
-
- json_test:json_test.cpp
- g++ -std=c++11 $^ -o $@ -ljsoncpp
结果如下:

二、
vi http_sever.cpp
-
- 1 #include "httplib.h"
- 2 using namespace std;
- 3 void HelloWord(const httplib::Request &req,httplib::Response &rsp)
- 4 {
- 5 rsp.body="hello bit!!!123456";
- 6 rsp.status=200;
- 7 return;
- 8 }
- 9
- 10 void Numbers(const httplib::Request &req,httplib::Response &rsp)
- 11 {
- 12 string num=req.matches[1];
- 13 rsp.body=num;
- 14 rsp.status=200;
- 15 }
- 16
- 17 void Dish(const httplib::Request &req,httplib::Response &rsp)
- 18 {
- 19 rsp.body=req.body;
- 20 rsp.status=200;
- 21 }
- 22
- 23 int main()
- 24 {
- 25 httplib::Server srv;
- 26 srv.set_mount_point("/","./wwwroot");
- 27
- 28 srv.Get("/hi",HelloWord);
- 29 srv.Get(R"(numbers/(d+))",Numbers);
- 30 srv.Post("/dish",Dish);
- 31 srv.listen("0.0.0.0",19000);
- 32 return 0;
- 33 }
index.html
- 1
- 2
- 3 "UTF-8"/>
- 4
- 5
- 6
语文数学英语
- 7
- 8
目录结构:

浏览器输入192.168.164.137:19000/hi,页面显示hello bit!!!123456,如下图所示:

浏览器输入192.168.164.137:19000/numbers,页面显示123456,如下图所示:

浏览器输入192.168.164.137:19000/index.html,页面显示语文数学英语,如下图所示:

-
相关阅读:
quarkus依赖注入之七:生命周期回调
详解:驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server 建立安全连接。
计算GAN生成图像数据集的平均SSIM
数据结构e
二维码智慧门牌管理系统:提升社会治理与服务的全新解决方案
gdb多线程调试
Swagger3+knife4j的使用
Matlab程序
小程序自定义组件(附“我的“页面item组件代码)
Python爬虫-某网酒店数据
-
原文地址:https://blog.csdn.net/weixin_46153828/article/details/126254936