map string类型转object类型
时间: 2023-09-24 12:07:49 浏览: 128
在你提供的引用中,有一个方法可以实现将String类型转换为Map<String, Object>类型的功能。你可以使用getValue方法来实现这个转换,这个方法接受一个String类型的参数,并返回一个Map<String, Object>类型的结果。具体代码如下:
```java
public static Map<String, Object> getValue(String param) {
Map<String, Object> map = new HashMap<>();
String str = "";
String key = "";
Object value = "";
char[] charList = param.toCharArray();
boolean valueBegin = false;
for (int i = 0; i < charList.length; i++) {
char c = charList[i];
if (c == '{') {
if (valueBegin == true) {
value = getValue(param.substring(i, param.length()));
i = param.indexOf('}', i) + 1;
map.put(key, value);
}
} else if (c == '=') {
valueBegin = true;
key = str;
str = "";
} else if (c == ',') {
valueBegin = false;
value = str;
str = "";
map.put(key, value);
} else if (c == '}') {
if (str != "") {
value = str;
}
map.put(key, value);
return map;
} else if (c != ' ') {
str += c;
}
}
return map;
}
```
使用这个方法,你可以将一个String类型的map转换成一个Map<String, Object>类型的对象。只需要调用getValue方法,并给它传递一个String类型的参数,它会返回一个Map<String, Object>类型的结果。你可以像这样使用它:
```java
String string = "{aaa=11, bbb=22, ccc=33, ddd=44}";
Map<String, Object> stringToMap = getValue(string);
for(Object key : stringToMap.keySet()) {
System.out.println(key + ": " + stringToMap.get(key));
}
```
这样,你就可以将一个String类型的map转换为一个Map<String, Object>类型的对象,并且可以对其进行操作和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [将String类型转换成Map数据类型](https://blog.csdn.net/m0_54850825/article/details/123710782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [java 读取csv 并转为Map<String,Object>](https://download.csdn.net/download/qq_37181642/87824694)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文