2 qq 31505613 qq_31505613 于 2017.12.07 21:47 提问

前端向后端传递复杂json类型遇到的问题 20C

$("#btn").click(function(){
var data={
header:{appId:'erghege',secret:'gergergeg'},
user:{name:'jery',pasword:'1132332'}
}
$.ajax({
url:"testjson.spring",
data:data,
dataType:"json",
contentType:"application/json",
success:function(){
}
});
我封装了一个类
public class Meta {
private User user;
private Header header;
//......
}
@RequestMapping("/testjson")
@ResponseBody
public Map logout(Meta meta){
Map map = new HashMap();
map.put("status", "1");
return map;
}
让Meta 接收前台传递过来的 json,报Invalid property 'header[appId]' of bean class [com.test.Meta]: Property referenced in indexed property path 'header[appId]' is neither an array nor a List nor a Map; returned value was [erghege]
有什么方法解决一下么

3个回答

qq_30866227
qq_30866227   2017.12.07 22:05

把data用json.stringify方法转化,再给后台方法接受的类前加上@requestBody

wb_snail
wb_snail   2017.12.07 22:20

var data={
header:{appId:'erghege',secret:'gergergeg'},
user:{name:'jery',pasword:'1132332'}
}
var data={};
var header={};
var user={};
user.name='jery';
user.pasword='1132332';
header.appId='erghege';
header.secret='gergergeg';
data.header=header;
data.user=user;
参数这样搞,然后ajax请求dataType:"json",
contentType:"application/json",这两句去掉试试

zhuoweisheng
zhuoweisheng   2017.12.08 15:16

@RequestMapping("/testjson")
@ResponseBody
public Map logout(Meta meta){
Map map = new HashMap();
map.put("status", "1");
return map;
}

修改为:

@RequestMapping("/testjson")
@ResponseBody
public Map logout(@RequestBody Map datas){
//这里对json的解悉
JSONObject req_json = (JSONObject)JSONObject.toJSON(datas);
Map map = new HashMap();
map.put("status", "1");
return map;
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!