我正在使用以下代码。在Controller中获取JSON列表的正确方法是什么?
以下JSON表单获取Controller:
method One: {"sets":[{"billitemId":"250","amount":"1212"},{"billitemId":"245","amount":"1212"},{"billitemId":"250","amount":"1212"},{"billitemId":"245","amount":"1212"}]}
Method Two: [{"billitemId":"250","amount":"1212"},{"billitemId":"245","amount":"1212"},{"billitemId":"250","amount":"1212"},{"billitemId":"245","amount":"1212"}]
JSP代码
var itemvalue;
var items =[];
function saveClearedDeatils(){
$('#esiTable').find('tr').each(function () {
var row = $(this);
if (row.find('input[type="checkbox"]').is(':checked'))
{
var billitemId=row.find('input[type="checkbox"]').val();
var amt = row.find('input[type="text"]').val();
itemvalue={"billitemId":billitemId,"amount":amt};
items.push(itemvalue);
//alert(JSON.stringify(itemvalue));
/* items.push({
"item" : billitemId,
"amount" : amt
}); */
}
});
var sets={"sets": items};
$.post("dummyurl?sets="+JSON.stringify(sets),
function(data){
alert("sldmhf");
});
/* $.ajax({
url: 'dummyurl',
type: 'POST',
data: JSON.stringify(items),
success: function(msg) {
alert(msg);
}
}); */
}
IN控制器:
@RequestMapping(value="/dummyurl",method= RequestMethod.POST)
@ResponseBody
public String dummy(@RequestParam("sets") String sets){
ObjectMapper mapper=new ObjectMapper();
try {
List<Object>arrayJSON=mapper.readValue(sets, new TypeReference<List<ArrayJSON>>() {});
System.out.println("fdkjvkfj");
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}