• jsoncpp库的使用及用httplib库搭建HTTP服务器


    一、 

    vi json_test.cpp 

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

    vi makefile

    1. http_sever:http_sever.cpp
    2. g++ -g -std=c++11 $^ -o $@ -lpthread
    3. json_test:json_test.cpp
    4. g++ -std=c++11 $^ -o $@ -ljsoncpp

     结果如下:

     二、

    vi http_sever.cpp

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

    index.html

    1. 1
    2. 2
    3. 3 "UTF-8"/>
    4. 4
    5. 5
    6. 6

      语文数学英语

    7. 7
    8. 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