前后端数据交互(json)

 参数前要加上@RequestBody注解,参数类型可以是JSONObject、String,entity.

 1 @RequestMapping(value="test",method=RequestMethod.POST,produces="application/json;charset=utf-8")
 2     @ResponseBody
 3     public Json test(@RequestBody JSONObject obj) {//JSONObject objMap<String,Object>
 4         Json json = new Json(true, "操作成功");
 5         Candidate candidate = JSON.parseObject(JSON.toJSONString(obj), Candidate.class);
 6         System.out.print("集合:"+candidate.getQueryList().size()+"	");
 7         System.out.println("数据:"+candidate.getStates().length+"	");
 8         System.out.println(candidate.toString());
 9         System.out.println(obj);
10         return json;
11     }

 测试的json字符串:

 1 {
 2     "id":"123",
 3     "type":"0",
 4     "birthday":"2020-11-17",
 5     "queryList":["44","33","22","11"],
 6     "states":[
 7       "q","w","e"
 8     ],
 9     "nominationRecommends":[
10         {
11         "id":"987",
12           "name":"张三"
13         },
14         {
15         "id":"789",
16           "name":"李四"
17         }
18         ]
19 
20 }

debuge:

结果:

原文地址:https://www.cnblogs.com/mxggx/p/13985916.html