java解析多层json数据

//这是restoken的值
{
    "EasyDarwin": {
        "Header": {
            "CSeq": "1",
            "Version": "v1",
            "MessageType": "MSG_SC_SERVER_GET_CHANNELS_CONFIG_ACK",
            "ErrorNum": "200",
            "ErrorString": "Success OK"
        },
        "Body": {
            "ChannelCount": 200,
            "Channels": [
                {
                    "Audio": 0,
                    "Cdn": "",
                    "CdnEnable": 0,
                    "Channel": 1,
                    "Enable": 1,
                    "IP": "",
                    "Name": "chinnel1",
                    "OnDemand": 0,
                    "Onvif": "",
                    "Password": "",
                    "Port": 0,
                    "Protocol": "RTSP",
                    "Record": 0,
                    "Reserve1": "0",
                    "Reserve2": "",
                    "Reserve3": "0",
                    "Reserve4": "0",
                    "Reserve5": "",
                    "Rtsp": "rtsp://dss/monitor/param?cameraid=1000113%247&substream=2",
                    "Rtsprecord": "",
                    "Transport": "TCP",
                    "UserName": ""
                },
                {
                    "Audio": 0,
                    "Cdn": "",
                    "CdnEnable": 0,
                    "Channel": 2,
                    "Enable": 1,
                    "IP": "",
                    "Name": "chinnel2",
                    "OnDemand": 0,
                    "Onvif": "",
                    "Password": "",
                    "Port": 0,
                    "Protocol": "RTSP",
                    "Record": 0,
                    "Reserve1": "0",
                    "Reserve2": "",
                    "Reserve3": "0",
                    "Reserve4": "0",
                    "Reserve5": "",
                    "Rtsp": "rtsp://dss/monitor/param?cameraid=1000113%246&substream=1",
                    "Rtsprecord": "",
                    "Transport": "TCP",
                    "UserName": ""
                },
                {
                    "Audio": 0,
                    "Cdn": "",
                    "CdnEnable": 0,
                    "Channel": 3,
                    "Enable": 1,
                    "IP": "",
                    "Name": "chinnel3",
                    "OnDemand": 0,
                    "Onvif": "",
                    "Password": "",
                    "Port": 0,
                    "Protocol": "RTSP",
                    "Record": 0,
                    "Reserve1": "0",
                    "Reserve2": "",
                    "Reserve3": "0",
                    "Reserve4": "0",
                    "Reserve5": "",
                    "Rtsp": "rtsp:///dss/monitor/param?cameraid=1000113%2410&substream=2",
                    "Rtsprecord": "",
                    "Transport": "UDP",
                    "UserName": ""
                },
                {
                    "Audio": 0,
                    "Cdn": "",
                    "CdnEnable": 0,
                    "Channel": 4,
                    "Enable": 1,
                    "IP": "",
                    "Name": "chinnel4",
                    "OnDemand": 0,
                    "Onvif": "",
                    "Password": "ubnt13572468",
                    "Port": 0,
                    "Protocol": "RTSP",
                    "Record": 0,
                    "Reserve1": "0",
                    "Reserve2": "",
                    "Reserve3": "0",
                    "Reserve4": "0",
                    "Reserve5": "",
                    "Rtsp": "rtsp://admin:ubnt13572468@02/Streaming/Channels/2?transportmode=unicast",
                    "Rtsprecord": "",
                    "Transport": "TCP",
                    "UserName": ""
                },
                {
                    "Audio": 0,
                    "Cdn": "",
                    "CdnEnable": 0,
                    "Channel": 5,
                    "Enable": 0,
                    "IP": "36.110.6.226",
                    "Name": "chinnel5",
                    "OnDemand": 1,
                    "Onvif": "",
                    "Password": "12345",
                    "Port": 11272,
                    "Protocol": "RTSP",
                    "Record": 0,
                    "Reserve1": "0",
                    "Reserve2": "",
                    "Reserve3": "0",
                    "Reserve4": "0",
                    "Reserve5": "",
                    "Rtsp": "rtsp://admin:12341272/h264/ch1/main/av_stream",
                    "Rtsprecord": "",
                    "Transport": "TCP",
                    "UserName": ""
                },
                {
                    "Audio": 0,
                    "Cdn": "",
                    "CdnEnable": 0,
                    "Channel": 6,
                    "Enable": 1,
                    "IP": "",
                    "Name": "chinnel6",
                    "OnDemand": 0,
                    "Onvif": "",
                    "Password": "",
                    "Port": 0,
                    "Protocol": "RTSP",
                    "Record": 0,
                    "Reserve1": "0",
                    "Reserve2": "",
                    "Reserve3": "0",
                    "Reserve4": "0",
                    "Reserve5": "",
                    "Rtsp": "rtsp://290/dss/monitor/param?cameraid=1000113%245&substream=2",
                    "Rtsprecord": "",
                    "Transport": "UDP",
                    "UserName": ""
                },
            ]
        }
    }
}
String restoken = (String) map.get("data");
		JSONObject jsonObject = JSONObject.parseObject(restoken);
		//获取第一层
		JSONObject A1 = jsonObject.getJSONObject("EasyDarwin");
		//获取第二层
		JSONObject A2 = A1.getJSONObject("Body");
		//获取第三层
		List<Object> Array = A2.getJSONArray("Channels");
		JSONArray jsonArray = new JSONArray(Array);
		List<Integer> using = new ArrayList<Integer>();
		//获取在用通道
		for(int i = 0;i<jsonArray.size();i++) {
			JSONObject jsonObject1 = jsonArray.getJSONObject(i);
			using.add(jsonObject1.getInteger("Channel"));
			System.out.println(jsonObject1.getInteger("Channel"));
		}
		

json字符串转 对象

/*
* 交易正常的返回
* jsonStr = {“status”:200,“method”:“GET”,“requestTime”:“0.003”}
*/
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
ApiConsumer resultInfo = (ApiConsumer) JSONObject.toBean(jsonObject,ApiConsumer.class);

jsonArray转 对象List

 String inventoryRes= "";//数据源
    JSONArray a = JSONArray.parseArray(inventoryRes);
    List<InventoryRes> inventoryResList = JSONArray.parseArray(a.toJSONString(),InventoryRes.class);

class ApiConsumer {

String status;    // 返回状态
String method;// 请求方法
String requestTime;// 响应时间

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值