当前位置:   article > 正文

beanshell把接口返回的数据保存下来_beanshell如何将接口的返回保温从字符串格式转换为json格式

beanshell如何将接口的返回保温从字符串格式转换为json格式
  1. //导入json包
  2. import com.alibaba.fastjson.*;
  3. //获取获取请求的返回值
  4. String response_data = prev.getResponseDataAsString();
  5. //日志打印获取请求的返回值
  6. log.info(response_data);
  7. //String类型的返回值构造成JSONObject对象
  8. JSONObject data_obj = JSONObject.parseObject(response_data);
  9. //获取data的值 并且转换成JSONObject对象
  10. data = data_obj.getString("data");
  11. JSONObject data_o = JSONObject.parseObject(data);
  12. //records转成数组
  13. JSONArray sign_array=data_o.getJSONArray("sign");
  14. JSONArray unSign_array=data_o.getJSONArray("unSign");
  15. //获取数组长度
  16. int s = sign_array.size();
  17. int u = unSign_array.size();
  18. int len = s+u;
  19. log.info(len.toString());
  20. // 先把文件里的内容清空
  21. //FileWriter clrstream=new FileWriter("C:/Users/Administrator/Desktop/proof.csv",false);//本地存储文件路径
  22. //
  23. //BufferedWriter clrout = new BufferedWriter(clrstream);
  24. //clrout.write("");
  25. //clrout.close();
  26. //clrstream.close();
  27. //再次打开文件写入这次运行的结果 先把文件打开
  28. FileWriter fstream=new FileWriter("C:/Users/Administrator/Desktop/proof.csv",true);//本地存储文件路径
  29. BufferedWriter out = new BufferedWriter(fstream);
  30. log.info("1");
  31. for(int i=0;i<s;i++){ //循环获取数组里需要的值
  32. JSONObject r = (JSONObject)sign_array.getJSONObject(i);//data里的每个json单独取出来 转换成JSONObject对象
  33. String taskId = r.get("taskId").toString(); //获取需要的值
  34. String userName = r.get("userName").toString();
  35. String idCard = r.get("idCard").toString();
  36. String qualiNo = r.get("qualiNo").toString();
  37. String certificateNo = r.get("certificateNo").toString();
  38. String certificateType = r.get("certificateType").toString();
  39. String graduationTime = r.get("graduationTime").toString();
  40. String itemType = r.get("itemType").toString();
  41. String major = r.get("major").toString();
  42. String graduationProofPdf = r.get("graduationProofPdf").toString();
  43. String graduationProofImg = r.get("graduationProofImg").toString();
  44. log.info(taskId);
  45. log.info(userName);
  46. log.info(idCard);
  47. log.info(qualiNo);
  48. log.info(certificateNo);
  49. log.info(certificateType);
  50. log.info(graduationTime);
  51. log.info(itemType);
  52. log.info(major);
  53. log.info(graduationProofPdf);
  54. // log.info(graduationProofImg);
  55. //写入文件
  56. out.write(taskId);
  57. out.write(",");
  58. out.write(userName);
  59. out.write(",");
  60. out.write(idCard);
  61. out.write(",");
  62. out.write(qualiNo);
  63. out.write(",");
  64. out.write(certificateNo);
  65. out.write(",");
  66. out.write(certificateType);
  67. out.write(",");
  68. out.write(",");
  69. out.write(graduationTime);
  70. out.write(",");
  71. out.write(itemType);
  72. out.write(",");
  73. out.write(major);
  74. out.write(",");
  75. out.write(graduationProofPdf);
  76. out.write(",");
  77. out.write(graduationProofImg);
  78. out.write(System.getProperty("line.separator"));
  79. }
  80. out.write(System.getProperty("line.separator")); //注册类 非注册类分开一行
  81. for(int i=0;i<u;i++){ //循环获取数组里需要的值
  82. JSONObject r = (JSONObject)unSign_array.getJSONObject(i);//data里的每个json单独取出来 转换成JSONObject对象
  83. String taskId = r.get("taskId").toString(); //获取需要的值
  84. String userName = r.get("userName").toString();
  85. String idCard = r.get("idCard").toString();
  86. String certificateNo = r.get("certificateNo").toString();
  87. String workTypeName = r.get("workTypeName").toString();
  88. String postName = r.get("postName").toString();
  89. String graduationTime = r.get("graduationTime").toString();
  90. String graduationProofPdf = r.get("graduationProofPdf").toString();
  91. String graduationProofImg = r.get("graduationProofImg").toString();
  92. log.info(taskId);
  93. log.info(userName);
  94. log.info(idCard);
  95. log.info(certificateNo);
  96. log.info(workTypeName);
  97. log.info(postName);
  98. log.info(graduationTime);
  99. log.info(graduationProofPdf);
  100. // log.info(graduationProofImg);
  101. //写入文件
  102. out.write(taskId);
  103. out.write(",");
  104. out.write(userName);
  105. out.write(",");
  106. out.write(idCard);
  107. out.write(",");
  108. out.write(",");
  109. out.write(certificateNo);
  110. out.write(",");
  111. out.write(workTypeName);
  112. out.write(",");
  113. out.write(postName);
  114. out.write(",");
  115. out.write(graduationTime);
  116. out.write(",");
  117. out.write(",");
  118. out.write(",");
  119. out.write(graduationProofPdf);
  120. out.write(",");
  121. out.write(graduationProofImg);
  122. out.write(System.getProperty("line.separator"));
  123. }
  124. //关闭文件
  125. out.close();
  126. fstream.close();

接口返回的数据格式如下

{
    "code": "000000",
    "msg": "success",
    "timestamp": "2024-04-24 09:45:06",
    "data": {
        "sign": [
            {
                "taskId": 7021,
                "userName": "**",
                "idCard": "530302199011012976",
                "qualiNo": "536211910422003381",
                "certificateNo": "云2532019202128859",
                "certificateType": "市政公用工程(主项)",
                "graduationTime": "2023-04-19 15:23:39",
                "itemType": 1,
                "major": "市政公用工程",
                "graduationProofPdf": "https://yn-isp.oss-cn-hangzhou.aliyuncs.com/pdf_a610ac9a-cc93-4da4-9e03-19e58e5261e3.pdf",
                "graduationProofImg": "https://yn-isp.oss-cn-hangzhou.aliyuncs.com/tar_2ee48607-9e7f-40d0-bf33-388dcc1ab2ea.jpg"
            }
        ],
        "unSign": [
            {
                "taskId": 102045095822,
                "userName": "**",
                "idCard": "**",
                "certificateNo": "云建安B(2024)**",
                "workTypeName": "安管三类人员",
                "postName": "项目负责人",
                "graduationTime": "2024-04-02 10:55:41",
                "graduationProofPdf": "https://eibp-prd.oss-cn-hangzhou.aliyuncs.com/**.pdf",
                "graduationProofImg": null,
                "isView": 1
            },
            {
                "taskId": 102044975875,
                "userName": "**",
                "idCard": "**",
                "certificateNo": "云A**",
                "workTypeName": "特种作业人员(省级)",
                "postName": "焊工",
                "graduationTime": "2024-03-31 12:36:49",
                "graduationProofPdf": "https://eibp-prd.oss-cn-hangzhou.aliyuncs.com/**.pdf",
                "graduationProofImg": null,
                "isView": 1
            }
            }
        ]
    }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/1014202
推荐阅读
相关标签
  

闽ICP备14008679号