Jackson form submit checkboxes
I have a form with checkboxes, on form submit. If one checked, it comes in as string, if multiple checked, it comes in as an json array.
How do I get the object as an array?
TypeReference<HashMap> typeRef = new TypeReference<HashMap>(){};
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
开发者_运维技巧 HashMap<String, Object> bean = mapper.readValue(formBean, typeRef);
bean.get("somevarible")
< I want to get this as array regardless how many boxes checked
Thanks
EDIT
I am using this function, how can I update it?
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
I had a similar issue deserializing json (aka "the checkbox problem"). My (ugly) work-around was to use @JsonAnySetter to manually do the "right" thing with the incoming (string/list) value.
// q is always just a string.
@JsonProperty("q")
private String query;
// fq could be a string or a list.
private List<String> filterQuery;
// (get/set/add omitted)
// XXX - this is an evil hack to support 1 or >1 fq values
// there must be a better way to support 2-way deserialization
@JsonAnySetter
void addEntry(String key, Object value)
{
if ("fq".equals(key))
{
if (value == null)
{
// can this even happen?
} else if (String.class.equals(value.getClass()))
{
addFilterQuery((String) value);
} else
{
setFilterQuery((List<String>) value);
}
}
}
精彩评论