将字符串转换为Map,可以使用以下方法:
import com.fasterxml.jackson.databind.ObjectMapper;
String jsonString = "{\"key1\":\"value1\", \"key2\":\"value2\"}";
ObjectMapper mapper = new ObjectMapper();
Map<String, String> map = mapper.readValue(jsonString, new TypeReference<Map<String, String>>(){});
System.out.println(map);
key1=value1;key2=value2
,可以使用以下代码将其转换为Map:String str = "key1=value1;key2=value2";
Map<String, String> map = new HashMap<>();
String[] keyValuePairs = str.split(";");
for (String keyValuePair : keyValuePairs) {
String[] keyValue = keyValuePair.split("=");
if (keyValue.length == 2) {
map.put(keyValue[0], keyValue[1]);
}
}
System.out.println(map);
这些方法根据字符串的格式不同选择适合的转换方法。希望能帮到您!