一、
vi json_test.cpp
#include<iostream>
2 #include<string>
3 #include<jsoncpp/json/json.h>
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<<str<<endl;
25
26 }
27
28
29 void UnSeralize()
30 {
31 string str=R"({"姓名":"李四","年龄":19,"成绩":[66,87.5,99]})";
32 cout<<str<<endl;
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<<val["姓名"].asString()<<endl;
43 cout<<val["年龄"].asInt()<<endl;
44 if(val["成绩"].isArray())
45 {
46 int sz=val["成绩"].size();
47 for(int i=0;i<sz;i++)
48 {
49 cout<<val["成绩"][i].asFloat()<<endl;
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 <html>
2 <head>
3 <meta charset="UTF-8"/>
4 </head>
5 <body>
6 <h1>语文数学英语</h1>
7 </body>
8 </html>
目录结构:
浏览器输入192.168.164.137:19000/hi,页面显示hello bit!!!123456,如下图所示:
浏览器输入192.168.164.137:19000/numbers,页面显示123456,如下图所示:
浏览器输入192.168.164.137:19000/index.html,页面显示语文数学英语,如下图所示: