我的实体包含atr1
,atr2
和atr3
或包含atr4
和atr5
的实体。
我想从地图类型
创建一个方法Map1 key atr4 => String list of value1, value2 value3
key atr5 => String list of value5, value6
和这个界面:
private <T extends Object> T parameterToList(Class<T> classOfT, Map<String,String[]> mapData)
给我一个List(T将是entity1或entity2)
是否可以使用gson?
答案 0 :(得分:0)
最后我找到了解决方案:
public <T extends Object> List<T> parameterToList( Map<String,String[]> entity, Class<T> classOfT) {
Map<String, String[]> collect = entity.entrySet().stream()
.filter(p -> p.getKey().startsWith(entity))
.collect(Collectors.toMap(p -> p.getKey().replace(entity + ".", ""), p -> p.getValue()));
JsonObject requestJson = new JsonObject();
JsonArray main = new JsonArray();
Map.Entry<String, String[]> entry1 = collect.entrySet().iterator().next();
String key = entry1.getKey();
int size = entry1.getValue().length;
int i = 0;
while (i < size) {
for (Map.Entry<String, String[]> entry : collect.entrySet()) {
// add every item as a property to the json
requestJson.addProperty(entry.getKey(), entry.getValue()[i]);
}
i = i + 1 ;
main.add(requestJson);
} List<T> fromJson = gson.fromJson(main, new TypeToken<List<T>>() {}.getType());
return fromJson;