java练习:json字符串转map、arrayList
使用依赖包:
<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.0</version> </dependency>
获取数据:
package com.example.myblog.entity; import lombok.Data; import java.util.Map; @Data public class MsgBody { private String eventId; private String patientId; private String content; private Map msgContentBody; private String eventProfessionType; private String nowTime; }
package com.example.myblog.service.impl; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONArray; import com.example.myblog.dao.MsgMapper; import com.example.myblog.entity.MsgBody; import com.example.myblog.service.MsgServe; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.UUID; @Service public class MsgServeImpl implements MsgServe { @Autowired @Resource private MsgMapper msgMapper; @Override public void saveMsg(MsgBody msgBody) { msgBody.setEventId(UUID.randomUUID().toString().replace("-","")); msgMapper.saveMsg(msgBody); } @Override public ArrayList<MsgBody> getMsg() { ArrayList<MsgBody> list =msgMapper.getMsg(); for (int i = 0; i < list.size(); i++) { String content = list.get(i).getContent(); JSONObject msgContentBody = JSONObject.parseObject(content,null); String remindRule = String.valueOf(msgContentBody.get("remindRule")); JSONArray remind = JSON.parseArray(remindRule); msgContentBody.put("remindArrayRule",remind); String eduID = String.valueOf(msgContentBody.get("eduID")); if(!"".equals(eduID)){ msgContentBody.put("eduArray",eduID.split(",")); }else{ msgContentBody.put("eduArray",new ArrayList<String>()); } list.get(i).setMsgContentBody(msgContentBody); } return list; } }
保存的数据格式:
{ "content": "{\"reminderAudio\":\"/resource/audio/edArticle/6f60e73abd0d47b4b679e823e1ec335a.mp3\",\"contentPath\":\"/resource/audio/edArticle/6f60e73abd0d47b4b679e823e1ec335a.mp3\",\"msgType\":\"宣教推送消息\",\"isBroadcasting\":0,\"isGenerateAudio\":1,\"type\":3,\"onlyFlag\":\"PUSH-EDU-MSG\",\"remindRule\":\"[{\\\"time\\\":1},{\\\"time\\\":\\\"2\\\"}]\",\"content\":\"手术手术手术手术手术。\",\"broadcastName\":\"宣教推送消息\",\"eduID\":\"933bce53ba3645f89747351d9e193ce0,a6a04b9516c64876a438e4e38dfca609\",operationTime:\"2023-12-01 15:30:00\"}" }
返回结果:
{ "eventId": "7411c79dcd084812bef5fbd8262849de", "patientId": "patientId111", "content": "{\"reminderAudio\":\"/resource/audio/edArticle/6f60e73abd0d47b4b679e823e1ec335a.mp3\",\"contentPath\":\"/resource/audio/edArticle/6f60e73abd0d47b4b679e823e1ec335a.mp3\",\"msgType\":\"宣教推送消息\",\"isBroadcasting\":0,\"isGenerateAudio\":1,\"type\":3,\"onlyFlag\":\"PUSH-EDU-MSG\",\"remindRule\":\"[{\\\"time\\\":1},{\\\"time\\\":\\\"2\\\"}]\",\"content\":\"手术手术手术手术手术。\",\"broadcastName\":\"宣教推送消息\",\"eduID\":\"933bce53ba3645f89747351d9e193ce0,a6a04b9516c64876a438e4e38dfca609\",operationTime:\"2023-12-01 15:30:00\"}", "msgContentBody": { "reminderAudio": "/resource/audio/edArticle/6f60e73abd0d47b4b679e823e1ec335a.mp3", "contentPath": "/resource/audio/edArticle/6f60e73abd0d47b4b679e823e1ec335a.mp3", "msgType": "宣教推送消息", "isBroadcasting": 0, "isGenerateAudio": 1, "type": 3, "onlyFlag": "PUSH-EDU-MSG", "remindRule": "[{\"time\":1},{\"time\":\"2\"}]", "content": "手术手术手术手术手术。", "broadcastName": "宣教推送消息", "eduID": "933bce53ba3645f89747351d9e193ce0,a6a04b9516c64876a438e4e38dfca609", "operationTime": "2023-12-01 15:30:00", "remindArray": [ { "time": 1 }, { "time": "2" } ], "eduArray": [ "933bce53ba3645f89747351d9e193ce0", "a6a04b9516c64876a438e4e38dfca609" ] }, "eventProfessionType": "PUSH-EDU-MSG", "nowTime": "2023-11-30 11:30:24" }
其中,将content解析为msgContentBody,content里面的remindRule解析为remindArray,eduID解析为eduArray
业务场景整体描述:
定义好多个消息规则,一群人中有人触发了规则,发一条消息,消息有重复提醒机制【remindRule】,触发的消息保存数据库。
重复提醒逻辑:
定时遍历这个数据库,修改【remindRule】,当【remindRule[0].time】变为0,则发消息再次提醒删除【remindRule[0]】,否则【remindRule[0].time-10】
【remindRule.length===0】说明重复提醒完毕,删除这条消息。
已读提醒逻辑:
【eduID】记录多个文章id,循环中根据文章id和人员id获取是否已读,已读则删除【eduID】中这条文章id
【eduID】变为空,说明阅读完成,没必要再次提醒,删除这条消息。