微信小程序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);
posted @ 2020-08-29 16:37  boxJLP  阅读(836)  评论(0)    收藏  举报