ajax post请求json数据在spring-controller解析

1、前端post请求数据:

userInfo=[{"id":"5","uname":"小李","phone":"guizhou","address":"guizhou","header":"null","motto":"baskteball","nickname":"ku","hobbies":"null"},{"id":"6","uname":"小花","phone":"guizhou","address":"guizhou","header":"null","motto":"baskteball","nickname":"ku","hobbies":"null"}], 
$.ajax({
        url:"user/saveAmend",
        type:"post",
        data:{
            "userList":JSON.stringify(userInfo)
        },
        // contentType: 'application/json;charset=utf-8',
        //traditional:true,
        //dataType: "json",
        success:function () {
            // var dt = JSON.parse(result);
            console.log("ok")
        },
        errorr:function () {
            console.log("服务器出现异常!")
        }
    })

  

2、在contorller里面解析 

@RequestMapping(value = "/saveAmend",method = RequestMethod.POST)
    //@ResponseBody
    public void saveAmend(@RequestParam(value = "userList") String userList,
                          HttpServletResponse response) throws IOException {
        //修改接收的内容。
        System.out.println(userList);

        List<Map<String,String>> uList = (List<Map<String, String>>) JSONObject.parse(userList);
        System.out.println(uList.size());


//        ObjectMapper mapper = new ObjectMapper();
//        response.getWriter().write(mapper.writeValueAsString(true));
//        response.getWriter().close();
//        return "success";
    }

  

3、在配置maven的 pom.xml里面导入阿里json jar包

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.56</version>
    </dependency>

  

  最后我想说一句日了狗了,这个问题搞了我一早上。

原文地址:https://www.cnblogs.com/wuzaipei/p/10619922.html