Map类型的字符串如何转换为map对象

mac2024-03-12  24

import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Map类型的字符串如何转换为map对象 * */ public class Dome { public static void main(String[] args) { String url = "{rtmpUrl=rtmp://live.guge.com/i11vk/aiw111eike?auth_key=1572228092-0-0-def80730da6d911f4eeecd1c0f770130," + "flvUrl=http://live.baidu.com/ivk/aiweike.flv?auth_key=1572228092-0-0-7dccf568a13764bad2c173a42321f970," + "m3u8Url=http://live.taobao.com/ivk/aiweike.m3u8?auth_key=1572228092-0-0-6b8ef37459e5d3b1c967376fc55e96d8}"; Map<String,Object> map = StrinInterceptsTheUrlPath(url); System.out.println("map --- " + map);// System.out.println("map --- " + map.get("flvUrl")); } /** * 将字符串截取url路径返回 * @param 字符串 * @return 字符串URL */ public static Map<String,Object> StrinInterceptsTheUrlPath(String url){ Map<String,Object> map = new HashMap<> (); if(url.length() > 0){ //将字符串转换为StringBuilder,并将字符串两头的花括号删掉 StringBuilder sb = new StringBuilder(url); sb.delete(0, 1).delete(sb.length()-1,sb.length()); //将StringBuilder转换为字符串,接着转换为List数组 String str = sb.toString(); List<String> strList = Arrays.asList(str.split(",")); //循环数组 for (int i = 0; i < strList.size(); i++) { //得到字符串第一个等号的下标 int index = strList.get(i).indexOf("="); //截取字符串等号前半部分 做为map的key。 String key = strList.get(i).substring(0, index); System.out.println(key); //截取字符串等号后半部分,作为map的value; String value = strList.get(i).substring(index+1, strList.get(i).length()); map.put(key, value); } } return map; } }
最新回复(0)