从前端传来的JSON中获取数据
程序开发
2023-09-08 14:35:14
首先推荐一个神器,JSON在线解析及格式化验证 - JSON.cn 里面的“JSON在线解析”和“JSON生成JAVA实体”两个功能,前几天可是帮了我大忙了。
前几天写一个功能,在这个功能中前端传过来的JSON十分复杂,示例如:
{"DispositionNotificationListObject":{"DispositionNotificationObject":[{"AlarmDetailReason":"701","AlarmLevel":3,"AlarmLevelColor":"#ffffff00","AlarmLevelDesc":"ordinary","AlarmLevelName":"","AlarmLevelValue":50,"AlarmLinkageInfo":{"actionList":[{"actionBranch":0,"actionType":4,"devList":[{"devCode":"02079188091993080101"}],"globalParam":"99"}]},"AlarmMatch":0,"AlarmRuleType":99,"AnalysisTaskID":"","BehaviorAnalysisObject":{"BehaviorId":"10077","DeviceID":"02079188091993080101","ObjectID":"3553028120241925498","OccuredTime":"20220606172725","RuleType":1,"SubImageList":{"SubImageInfoObject":[{"Data":"","DeviceID":"02079188091993080101","EventSort":12,"FileFormat":"Jpeg","Height":1080,"ImageID":"60606812413199","ShotTime":"20220606172725","StoragePath":"","Type":"14","Width":1920}]},"metaData":{"attributes":{"alarmFlag":1,"detectObject":{"objPositionInfos":[[{"bottom":186,"left":1505,"right":1532,"top":123,"type":98}]],"posNum":1},"line":{"lineInfos":[{"direct":0,"endPoint":{"x":1538,"y":1076},"startPoint":{"x":1538,"y":82},"type":1}],"lineNum":1},"ruleId":0}}},"DispositionCategory":"1086","DispositionID":"40288be480e15c2022060617272557355","DispositionTargetID":"","FileID":"0","FileStrID":"","FromRedGroup":0,"GroupID":"0","GroupStrID":"","IsUserDefined":0,"NotificationID":"40288be480e15c2022060617272557355","NotificationIDEx":"2022060617272557355","SourceType":4,"SubscribeID":"40288be480e15c130180fa05f9bb662c","SuspectID":"0","SuspectStrID":"","TargetID":"0","TargetStrID":"","TaskUser":"","Title":"biuu","TriggerTime":"20220606172725315"}]}
}
可以发现,推过来的这个JSON不仅复杂,而且有一点十分让人在意,那就是它的属性值首字母基本上都是大写。这也就使得通过“JSON生成JAVA实体”获得的Java无法被直接使用。
有两种解决方法
(1)通过 JSONObject 。 在接收前端传来的JSON数据时,以String的形式接收,然后利用JSONObject 将说需要的的数据从一大串JSON中获取出来。(不同包的 JSONObject 使用方法不同,但都能实现相同的效果 )
示例:
//先把String对象转换成json对象JSONObject object1= new JSONObject(bodyStr);JSONObject DispositionNotificationListObject = object1.getJSONObject("DispositionNotificationListObject");JSONArray DispositionNotificationObjectArr = DispositionNotificationListObject.getJSONArray("DispositionNotificationObject");for (int i = 0; i < DispositionNotificationObjectArr.length(); i++) {JSONObject DispositionNotificationObject = DispositionNotificationObjectArr.getJSONObject(i);alarmLevelValue = ""+DispositionNotificationObject.getInt("AlarmLevelValue");alarmLevelColor = DispositionNotificationObject.getString("AlarmLevelColor");alarmType = DispositionNotificationObject.getString("AlarmDetailReason");alarmEventId = DispositionNotificationObject.getString("NotificationID");JSONObject BehaviorAnalysisObject = DispositionNotificationObject.getJSONObject("BehaviorAnalysisObject");occurTime = BehaviorAnalysisObject.getString("OccuredTime");alarmInCode = BehaviorAnalysisObject.getString("DeviceID");log.warn("开始获取picData");JSONObject SubImageList = BehaviorAnalysisObject.getJSONObject("SubImageList");JSONArray SubImageInfoObjectArr = SubImageList.getJSONArray("SubImageInfoObject");for (int j = 0; j < SubImageInfoObjectArr.length(); j++) {JSONObject SubImageInfoObject = SubImageInfoObjectArr.getJSONObject(j);picData = SubImageInfoObject.getString("Data");
// log.warn("picData");}
// log.warn("获取picData结束"+picData);}
(2)通过 @JsonProperty( value ="Name" ) 指定属性名,来避免首字母大写带来的错误
若不生效,可以试着同时再加上一个 @JSONField(name = "SubscribeID")
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonProperty注解的使用 - Mop猎人 - 博客园
@JSONField的一些使用基础_狂奔的蜗牛Evan的博客-CSDN博客_jsonfield
示例:
@JSONField(name = "SubscribeID")@JsonProperty( value ="SubscribeID" )private String SubscribeID;
postman传到后端的数据:、
标签:
上一篇:
nginx反代出现css/js加载失败
下一篇:
相关文章
-
无相关信息