java 中 json转 map 获取属性

44 篇文章 74 订阅
35 篇文章 1 订阅
package com.bonc.jiu.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: TestJson
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/01/15 15:55:37 
 * @Version: V1.0
 **/
public class TestJson {
    public static void main(String[] args) throws IOException {
        //String  msg="{\"method\":\"OnEventNotify\"}";
        Object  msg="{\"method\":\"OnEventNotify\",\"params\":{\"ability\":\"\",\"events\":[{\"eventDetails\":[{\"ability\":\"event_vss\",\"data\":{\"channelID\":1,\"dataType\":\"behavioralAnalysis\",\"dateTime\":\"2021-01-15T17:03:12.000+08:00\",\"eventDescription\":\"regionEntrance\",\"eventType\":\"regionEntrance\",\"ipAddress\":\"101.192.66.51\",\"portNo\":8000,\"recvTime\":\"2021-01-15T17:03:14.138+08:00\",\"regionEntrance\":[{\"detectionTarget\":1,\"imageUrl\":\"http://10.234.16.75:6120/pic?=d82i0d7e*a36i03a-c40671e0=t1i1m*dp=*2pdi=*1s9i5i6d6=*7b00a355f-f967470-4b1029-90z795s=6103d0\",\"regionCoordinatesList\":[{\"positionX\":0.1979999989271164,\"positionY\":0.09600000083446504},{\"positionX\":0.2370000034570694,\"positionY\":0.5609999895095825},{\"positionX\":0.449999988079071,\"positionY\":0.453000009059906},{\"positionX\":0.4210000038146973,\"positionY\":0.05900000035762787}],\"targetAttrs\":{\"cameraIndexCode\":\"d392df456ebe4532a002152dd8980712\",\"channelName\":\"#5号机6KV厂用\",\"deviceIndexCode\":\"4a55ab82cea449dc91701d6141993d35\",\"imageServerCode\":\"35ca3a85-b284-4c24-b2ad-357ad8cd987f\"}}],\"sendTime\":\"2021-01-15T17:03:14.143+08:00\"},\"eventOriginalId\":\"261F040D-D4D8-46B6-BD49-AE32A6AE1811\",\"eventType\":131586,\"locationIndexCode\":\"\",\"locationName\":\"\",\"regionIndexCode\":\"7377c6ca-8b2b-4aa5-bd7f-112f48e8ab19\",\"regionName\":\"九江电厂/视频监控/机房/三期\",\"srcIndex\":\"d392df456ebe4532a002152dd8980712\",\"srcName\":\"#5号机6KV厂用\",\"srcType\":\"camera\"}],\"eventId\":\"9A71CD479D59704C91842C302116EFC0\",\"eventLvl\":2,\"eventName\":\"周界入侵通知监控中心 模板\",\"eventOldId\":\"261F040D-D4D8-46B6-BD49-AE32A6AE1811\",\"eventType\":0,\"happenTime\":\"2021-01-15T17:03:12.000+08:00\",\"remark\":\"\",\"ruleDescription\":\"\",\"srcIndex\":\"2f67ce55a25c452ea7dcac11da7e4d60\",\"srcName\":\"周界入侵通知监控中心 模板\",\"srcParentIndex\":\"4a55ab82cea449dc91701d6141993d35\",\"srcType\":\"eventRule\",\"status\":0,\"stopTime\":\"2021-01-15T17:03:12.000+08:00\",\"timeout\":0}],\"sendTime\":\"2021-01-15T17:03:14.144+08:00\"}}\n";
         String dataJson=msg.toString();

        //2.获取事件视频流的类型code
        Map infoMap = new ObjectMapper().readValue(dataJson, Map.class); //json转换成map
        Map paramsMap=((Map)infoMap.get("params"));
        Map eventsMap=(Map)((List)paramsMap.get("events")).get(0);
        Map eventDetailsMap=(Map)((List)eventsMap.get("eventDetails")).get(0);
        Map dataMap=(Map)eventDetailsMap.get("data");
        Map regionEntranceMap=(Map)((List)dataMap.get("regionEntrance")).get(0);
        Map targetAttrsMap=(Map)regionEntranceMap.get("targetAttrs");
        String cameraIndexCode=(String)targetAttrsMap.get("cameraIndexCode");
        System.out.println("cameraIndexCode:"+cameraIndexCode);
        String sendTime=(String)paramsMap.get("sendTime");
        String eventType=eventDetailsMap.get("eventType")+"";
        System.out.println("sendTime:"+sendTime);
        System.out.println("eventType:"+eventType);

    }
}

结果:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值