Using String to convert to Map structure
- 2021-12-09 08:55:33
- OfStack
String conversion to Map architecture
The following is limited to individual tests
Recently, I encountered a problem in my work, that is, I need to put an Map < String, Object > This 1 type is saved, followed by the function of reading. Instead of using the usual serialization method, I remembered that Map. toString () could convert Map to String, but there was no corresponding reverse method.
I want to realize such a function, and I think it is good, so I paste the conversion code as follows, but there are many other ways to serialize map. This is just the reverse conversion of map. toString () implemented by myself:
public Object getValue(String param) {
Map 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;
}
Test case
From simple to complex
public void testFun() {
String str1 = "{idCard=123, phonenum=1234}";
String str2 = "{idCard=123, phonenum=1234, map={hhaha=haha}}";
String str3 = "{idCard=123, phonenum=1234, map={hhaha=haha}, nn={en=ha}}";
String str4 = "{nn={en=ha}, idCard=123, phonenum=1234, map={hhaha=ni, danshi={ke=shi}}}";
Map<String, Object> mapresutl1 = (Map<String, Object>) getValue(str1);
Map<String, Object> mapresutl2 = (Map<String, Object>) getValue(str2);
Map<String, Object> mapresutl3 = (Map<String, Object>) getValue(str3);
Map<String, Object> mapresutl4 = (Map<String, Object>) getValue(str4);
System.out.println(mapresutl1.toString());
System.out.println(mapresutl2.toString());
System.out.println(mapresutl3.toString());
System.out.println(mapresutl4.toString());
}
Output:
{idCard=123, phonenum=1234} {idCard=123, phonenum=1234, map={hhaha=haha}} {nn={en=ha}, idCard=123, phonenum=1234, map={hhaha=haha}} {nn={en=ha}, idCard=123, phonenum=1234, map={hhaha=ni, danshi={ke=shi}}}
The function is capable of handling Map < String, Object > The string of. toString is flipped to the corresponding Map again, where Object can only be Map type or other basic types. If it is complex, it is not involved here, or the complex structure can be represented by the key-value pair of Map, so it can be used in this way.
Later, we found that there are many ways to serialize, so it is not necessary to implement one by ourselves, and map can also be serialized
The following serialization methods
java comes with, json, hession
There are also Ali's fastjson, protobuff and so on
All of the above can realize the serialization of map
Special format of String to Map
String a ="{se=2016, format=xml, at=en co=3}";
a = a.substring(1, a.length()-1);
Map docType = new HashMap();
java.util.StringTokenizer items;
for(StringTokenizer entrys = new StringTokenizer(a, ", ");entrys.hasMoreTokens();
docType.put(items.nextToken(), items.hasMoreTokens() ? ((Object) (items.nextToken())) : null)){
items = new StringTokenizer(entrys.nextToken(), "=");
}
System.out.println(docType);
System.out.println("a:"+docType.get("a"));
It is not necessary to use JSONArray or JSONObject as the transfer intermediary for processing, and String is directly transferred to Map