JAVA 恶心的字符串(字符串有点复杂)转实体类
Posted wwr01
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 恶心的字符串(字符串有点复杂)转实体类相关的知识,希望对你有一定的参考价值。
直接上示例:
字符串:
{"msg":{"absTime":1589882783000,"adminList":[],"alarmAddress":"沪亭南路姚北路北侧南向北1HG","alarmLevel":"1","alarmStatus":"0","alarmTime":1589882783000,"alarmType":"2","deviceId":"5d7c562f-f31b-4f25-a306-72358341ee63","ext":"[{\\"birthday\\":\\"未知\\",\\"libId\\":\\"550\\",\\"nation\\":\\"未知\\",\\"sex\\":\\"0\\",\\"libName\\":\\"布控中心重点人员 - T3\\",\\"remark\\":\\"级联布控人员告警\\",\\"targetImage\\":\\"http://15.210.56.40:6120/pic?2ddf32i6c-e*292198143b8b--6df4b688b14e9i3b2*=9d5s5*=0dpi*=1d5i7t5pe*m5i7-=9906485zcd8s=8i01=\\",\\"extend\\":\\"{\\\\\\"DealWith\\\\\\":\\\\\\"现场风洞道口任务,无需处置\\\\\\",\\\\\\"Reason\\\\\\":\\\\\\"支撑现场风洞道口任务\\\\\\",\\\\\\"Source\\\\\\":\\\\\\"刘迪冰\\\\\\"}\\",\\"sceneImg\\":\\"http://15.209.59.111:8080/SJKKPIC_1400/CJ/20200519/31011710051321035012/310117100513210350120220200519180623269100.jpg\\",\\"score\\":94,\\"createTime\\":\\"2020-05-19 18:09:55\\",\\"idcard\\":\\"440922197305180614\\",\\"name\\":\\"刘关平\\",\\"personId\\":\\"310117100513210350120220200519180623396370663590\\"}]","faceId":"310117100513210350120220200519180623396370663590","gbId":"31011710051321035012","id":"a376886d-90e7-47bd-92a9-9d87916ff5bc","idCard":"440922197305180614","imageUrl":"http://15.209.59.114:8080/SJKKPIC_1400/OTHER/20200519/31011710051321035012/310117100513210350120220200519180623269131.jpg","latitude":31.112101,"libId":"550","libName":"布控中心重点人员 - T3","loginId":"0ad29676-2e1d-40ca-b44a-ede03a1870","longitude":121.333601,"name":"刘关平","number":0,"realAlarm":-1,"recordId":"310117100513210350120220200519180623396370663590","score":94,"sex":"0","source":"vdt","status":"0","targetImage":"http://15.210.56.40:6120/pic?2ddf32i6c-e*292198143b8b--6df4b688b14e9i3b2*=9d5s5*=0dpi*=1d5i7t5pe*m5i7-=9906485zcd8s=8i01=","taskId":""},"systemMessage":"0","alarmId":"a376886d-90e7-47bd-92a9-9d87916ff5bc","type":"FACE_ALARM","userId":"b2d589d1-4b66-4d11-9960-103b98596d1a","gisMessage":"0"}
json格式(部分,展示不全):
定义对应的实体类:
public class AlarmFeedBack { public Msg msg; public String systemMessage; public String alarmId; public String type; public String userId; public String gisMessage; } public class Msg { public long absTime; public Object[] adminList; public String alarmAddress; public String alarmLevel; public String alarmStatus; public String alarmTime; public String alarmType; public String deviceId; public List<Ext> ext; public String faceId; public String gbId; public String id; public String idCard; public String imageUrl; public double latitude; public String libId; public String libName; public String loginId; public double longitude; public String name; public Integer number; public Integer realAlarm; public String recordId; public Integer score; public String sex; public String source; public String status; public String targetImage; public String taskId; } public class Ext { public String birthday; public String libId; public String nation; public String sex; public String libName; public String remark; public String targetImage; public Extend extend; public String sceneImg; public Integer score; public String createTime; public String idcard; public String name; public String personId; } public class Extend { String DealWith; String Reason; String Source; }
(重点在此)
解析方法:
/** * 解析 * @param str * @return */ public static AlarmFeedBack GetAlarmFeedBackFromStr(String str){ Map<String,String> map= com.alibaba.fastjson.JSONObject.parseObject(str,Map.class); Map<String,String> map1= com.alibaba.fastjson.JSONObject.parseObject(JSONObject.toJSONString(map.get("msg")),Map.class); String res=map1.get("ext"); AlarmFeedBack alarmFeedBack= JSONUtil.toBean(str,AlarmFeedBack.class); if(res!=null&&res.length()>0){ List<Map> maps=JSONObject.parseArray(res,Map.class); String ss= maps.get(0).get("extend").toString(); List<Ext>exts=JSONUtil.toList(JSONUtil.parseArray(res),Ext.class); alarmFeedBack.getMsg().setExt(exts); if(ss!=null&&ss.length()>0){ Extend extend=JSONUtil.toBean(ss,Extend.class); alarmFeedBack.getMsg().getExt().get(0).setExtend(extend); } } return alarmFeedBack; }
测试调用:
String str="{\\"msg\\":{\\"absTime\\":1589882783000,\\"adminList\\":[],\\"alarmAddress\\":\\"沪亭南路姚北路北侧南向北1HG\\",\\"alarmLevel\\":\\"1\\",\\"alarmStatus\\":\\"0\\",\\"alarmTime\\":1589882783000,\\"alarmType\\":\\"2\\",\\"deviceId\\":\\"5d7c562f-f31b-4f25-a306-72358341ee63\\",\\"ext\\":\\"[{\\\\\\"birthday\\\\\\":\\\\\\"未知\\\\\\",\\\\\\"libId\\\\\\":\\\\\\"550\\\\\\",\\\\\\"nation\\\\\\":\\\\\\"未知\\\\\\",\\\\\\"sex\\\\\\":\\\\\\"0\\\\\\",\\\\\\"libName\\\\\\":\\\\\\"布控中心重点人员 - T3\\\\\\",\\\\\\"remark\\\\\\":\\\\\\"级联布控人员告警\\\\\\",\\\\\\"targetImage\\\\\\":\\\\\\"http://15.210.56.40:6120/pic?2ddf32i6c-e*292198143b8b--6df4b688b14e9i3b2*=9d5s5*=0dpi*=1d5i7t5pe*m5i7-=9906485zcd8s=8i01=\\\\\\",\\\\\\"extend\\\\\\":\\\\\\"{\\\\\\\\\\\\\\"DealWith\\\\\\\\\\\\\\":\\\\\\\\\\\\\\"现场风洞道口任务,无需处置\\\\\\\\\\\\\\",\\\\\\\\\\\\\\"Reason\\\\\\\\\\\\\\":\\\\\\\\\\\\\\"支撑现场风洞道口任务\\\\\\\\\\\\\\",\\\\\\\\\\\\\\"Source\\\\\\\\\\\\\\":\\\\\\\\\\\\\\"刘迪冰\\\\\\\\\\\\\\"}\\\\\\",\\\\\\"sceneImg\\\\\\":\\\\\\"http://15.209.59.111:8080/SJKKPIC_1400/CJ/20200519/31011710051321035012/310117100513210350120220200519180623269100.jpg\\\\\\",\\\\\\"score\\\\\\":94,\\\\\\"createTime\\\\\\":\\\\\\"2020-05-19 18:09:55\\\\\\",\\\\\\"idcard\\\\\\":\\\\\\"440922197305180614\\\\\\",\\\\\\"name\\\\\\":\\\\\\"刘关平\\\\\\",\\\\\\"personId\\\\\\":\\\\\\"310117100513210350120220200519180623396370663590\\\\\\"}]\\",\\"faceId\\":\\"310117100513210350120220200519180623396370663590\\",\\"gbId\\":\\"31011710051321035012\\",\\"id\\":\\"a376886d-90e7-47bd-92a9-9d87916ff5bc\\",\\"idCard\\":\\"440922197305180614\\",\\"imageUrl\\":\\"http://15.209.59.114:8080/SJKKPIC_1400/OTHER/20200519/31011710051321035012/310117100513210350120220200519180623269131.jpg\\",\\"latitude\\":31.112101,\\"libId\\":\\"550\\",\\"libName\\":\\"布控中心重点人员 - T3\\",\\"loginId\\":\\"0ad29676-2e1d-40ca-b44a-ede03a187095\\",\\"type\\":\\"FACE_ALARM\\",\\"userId\\":\\"b2d589d1-4b66-4d11-9960-103b98596d1a\\",\\"gisMessage\\":\\"0\\"}"; AlarmFeedBack res= ParsePoliceSit.GetAlarmFeedBackToStr(str);
以上是关于JAVA 恶心的字符串(字符串有点复杂)转实体类的主要内容,如果未能解决你的问题,请参考以下文章