spring boot接收、处理json

json格式

{"uid":"root","role":[1,3,7]}

controller

@PostMapping("/add")
public Object AddRole(@RequestBody String post) {
    JSONObject jsonObject = new JSONObject(post);
    List<Object> roleIds = jsonObject.getJSONArray("role").toList();
    List<Integer> list=new ArrayList<>();
    for(Object roleId : roleIds){
        list.add(Integer.parseInt(roleId.toString()));
    }
    return null;
}

其他

导入的包为

import org.json.JSONObject;

后边才了解到,可以用以下方法获取,刚学spring boot,之前一直按照PHP的开发思路来的。。

@PostMapping("account")
public Object account(@RequestBody Map<String, Object> body) {
    String username = (String) body.get("username");
    String password = (String) body.get("password");
    AccountEntity accountEntity = accountService.login(username, password);
    if(accountEntity==null){
        return "用户名或密码错误";
    }
    return accountEntity;
}
posted @ 2023-08-31 23:12  小枫同学  阅读(252)  评论(0编辑  收藏  举报