"{length=1,size=2}"的string类型如何转为map
时间: 2024-03-11 11:50:40 浏览: 27
对于字符串"{length=1,size=2}",可以使用如下代码将其转换为Map:
```java
String str = "{length=1,size=2}";
Map<String, String> map = new HashMap<>();
String[] pairs = str.substring(1, str.length() - 1).split(",");
for (String pair : pairs) {
String[] keyValue = pair.split("=");
map.put(keyValue[0], keyValue[1]);
}
```
以上代码将字符串首尾的"{"和"}"去掉,然后按照","进行拆分,再按照"="进行拆分,最终将每个键值对存储到Map中。注意,这里的Map的键和值都是字符串类型。
相关问题
string 转map
假设字符串的格式为 "key1=value1&key2=value2&key3=value3",可以按照以下方式转换为 map:
```c++
#include <iostream>
#include <string>
#include <map>
int main() {
std::string str = "key1=value1&key2=value2&key3=value3";
std::map<std::string, std::string> kv_map;
size_t pos = 0;
while (pos < str.length()) {
size_t equal_pos = str.find('=', pos);
size_t amp_pos = str.find('&', pos);
if (equal_pos == std::string::npos) {
break;
}
std::string key = str.substr(pos, equal_pos - pos);
std::string value;
if (amp_pos == std::string::npos) {
value = str.substr(equal_pos + 1);
} else {
value = str.substr(equal_pos + 1, amp_pos - equal_pos - 1);
}
kv_map[key] = value;
if (amp_pos == std::string::npos) {
break;
}
pos = amp_pos + 1;
}
for (const auto& kv : kv_map) {
std::cout << kv.first << " => " << kv.second << std::endl;
}
return 0;
}
```
输出结果为:
```
key1 => value1
key2 => value2
key3 => value3
```
public static List<Map<String,Object>> hour(List<Map<String,Object>> list){ if(list != null && list.size()>0){ String item=""; String [] s1 =null; for(Map<String,Object> m : list){ item = m.get("item_info").toString(); s1 = item.split(";"); for(String s : s1){ String [] s2 = null; s2=s.split(","); m.put(s2[0],s2[2]); m.put(s2[0]+"code",s2[0]); m.put(s2[0]+"detname",s2[3]); if(s2.length>=5) { m.put(s2[0] + "bzz", s2[4]); } if(s2.length>=6) { m.put(s2[0] + "yj", s2[5]); } if(s2.length>=7) { m.put(s2[0] + "bj", s2[6]); } if(s2.length>=8) { m.put(s2[0] + "dw", s2[7]); } } } } return list; } 会报空指针异常 如何改正
可以在代码中加入对 `s2` 是否为 `null` 的判断,如下所示:
```
public static List<Map<String, Object>> hour(List<Map<String, Object>> list) {
if (list != null && list.size() > 0) {
String item = "";
String[] s1 = null;
for (Map<String, Object> m : list) {
item = m.get("item_info").toString();
s1 = item.split(";");
for (String s : s1) {
String[] s2 = null;
s2 = s.split(",");
if (s2 != null && s2.length > 2) {
m.put(s2[0], s2[2]);
m.put(s2[0] + "code", s2[0]);
m.put(s2[0] + "detname", s2[3]);
if (s2.length >= 5) {
m.put(s2[0] + "bzz", s2[4]);
}
if (s2.length >= 6) {
m.put(s2[0] + "yj", s2[5]);
}
if (s2.length >= 7) {
m.put(s2[0] + "bj", s2[6]);
}
if (s2.length >= 8) {
m.put(s2[0] + "dw", s2[7]);
}
}
}
}
}
return list;
}
```
在对 `s2` 进行操作前,加入了 `s2 != null && s2.length > 2` 的判断,确保了 `s2` 不为 `null` 且至少有 3 个元素,避免了空指针异常的发生。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)