微信小程序JSON请求(JAVA)

微信小程序存在着大量的HTTPS请求,通常是推送消息。那么我们后台就要对参数进行JSON格式封装,如:

{ "touser" : "zhangsan|lisi", "toparty": "1|2", "totag": "1|2", "msgtype" : "miniprogram_notice", "miniprogram_notice" : { "appid": "wx123123123123123", "page": "pages/index?userid=zhangsan&orderid=123123123", "title": "会议室预订成功通知", "description": "4月27日 16:16", "emphasis_first_item": true, "content_item": [ { "key": "会议室", "value": "402" }, { "key": "会议地点", "value": "广州TIT-402会议室" }, { "key": "会议时间", "value": "2018年8月1日 09:00-09:30" }, { "key": "参与人员", "value": "周剑轩" } ] }, "enable_id_trans": 0, "enable_duplicate_check": 0, "duplicate_check_interval": 1800 }

里面包括了对象和数组,JAVA后台如何封装?

JSONObject json = new JSONObject(); //对象 JSONObject jsonData = new JSONObject(); //子对象 JSONObject jsonChildData1 = new JSONObject(); JSONObject jsonChildData2 = new JSONObject(); JSONObject jsonChildData3 = new JSONObject(); //数组 JSONArray jsonArray = new JSONArray(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); json.put("touser", "nobleblood"); json.put("msgtype", "miniprogram_notice"); jsonData.put("appid", initJson(project.getProjectNo())); jsonData.put("title", initJson(project.getProjectNo())); jsonData.put("description", initJson(project.getProjectName().substring(0,20))); jsonData.put("emphasis_first_item", true); jsonChildData1.put("key", initJson(format.format(new Date()))); jsonChildData1.put("value", initJson(format.format(new Date()))); jsonChildData2.put("key", initJson(format.format(new Date()))); jsonChildData2.put("value",initJson(project.getReviewStatus().substring(0,5))); jsonChildData3.put("key", initJson(format.format(new Date()))); jsonChildData3.put("value",initJson(project.getReviewStatus().substring(0,5))); jsonArray.add(0,jsonChildData1); jsonArray.add(1,jsonChildData2); jsonArray.add(2,jsonChildData3); jsonData.put("content_item",jsonArray); json.put("miniprogram_notice",jsonData); json.put("data",jsonData);

__EOF__

本文作者boxJLP
本文链接https://www.cnblogs.com/toWorld/p/13582639.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   boxJLP  阅读(765)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示