我有POST查询字符串,格式如下:
参数1 = AAA&安培;内部件[0] [ “innerParam”] = BBB&安培;内部件[1] [ “innerParam”] = nnn的
我需要轻松将其转换为地图或POJO。
public class pojo{
private String param1;
private List<OtherPojo> inners;//array is also ok
//getters etc
}
class OtherPojo{
private String innerParam.
//getters etc
}
我认为这可能是由泽西@BeanParam或其他人做的,但不幸的是,这是不可能的。所以我只有一个字符串,需要将其编译为map或pojo。请注意,我不清楚如何解析这个结构
inners[0]["innerParam"]
我不想手动完成。所以我需要在一行中解析它。
Pojo p=someHelper.compileToPojo(postString);// or map
使用哪个lib,如果存在?
答案 0 :(得分:2)
您可以使用的库是:var el = $compile( "<"+scope.popupDirective+"></"+scope.popupDirective+">" )( scope );
以及如何实施:
com.fasterxml.jackson
因此,您应该能够实现自己的public void checkAndSetChildValues(ObjectNode node, String field, String value, ObjectMapper mapper) {
int indexDot = field.indexOf('.');
if (indexDot > -1) {
String childFieldName = field.substring(0, indexDot);
ObjectNode child = node.with(childFieldName);
checkAndSetChildValues(child, field.substring(indexDot + 1), value, mapper);
} else {
try{
node.set(field, mapper.convertValue(value, JsonNode.class));
} catch(IllegalArgumentException ex){
logger.debug("could not parse value {} for field {}", value, field);
}
}
}
public Object parse(Class type, String entityString) throws UnsupportedEncodingException {
ObjectMapper mapper = mapperHolder.get();
ObjectNode node = mapper.createObjectNode();
Scanner s = new Scanner(entityString).useDelimiter("&|=");
while (s.hasNext()) {
String key = s.next();
String value = s.hasNext() ? URLDecoder.decode(s.next(), "UTF-8") : null;
checkAndSetChildValues(node, key, value, mapper);
}
Object result = mapper.convertValue(node, type);
return result;
}
,请参阅:https://jersey.java.net/documentation/latest/message-body-workers.html#d0e7151