前台使用复杂json,后台直接封装到对象
当我们需要一次向后台传入多个数据和一个list对象的时候,显然用fromb表单是很不方便的.比如要传入一下的数据
//一个list
var customerArray = new Array();
customerArray.push({id: 123, foodid: "字符串可以", merchantid: 3423});
customerArray.push({id: 456, foodid: 234, merchantid: 554});
//list和几个其他的对象
exportData('/rest/FoodGroup/insertFoodGroup',{topicId:"蟑螂恶霸", topicName:"车轮滚滚", summarize:"蜻蜓队长", 'parentList':customerArray});
function exportData(url,params){
$.ajax({
'url':url,
'type':'post',
'data':JSON.stringify(params),
'dataType':'json',
'contentType':'application/json;charset=utf-8',
success:function(result){
},
error:function(res){
}
});
}
@Controller接收
@RequestMapping("/insertFoodGroup")
@ResponseBody
public String insertFoodGroup(@RequestBody Map<String,Object> json )
{
//先保存一份菜品.返回菜品id
//在保存分组和当前分组的菜品
Object object = json.get("parentList");//拿到list对象
//(这里的JSONArray用的是阿里的fastjson)
List<TestBean> s= new ArrayList<TestBean>(JSONArray.parseArray( JSON.toJSONString(object),TestBean.class));
return null;
}
testBean
public class TestBean {
private Integer id;
private String foodid;
private String merchantid;
//getset...
}