使用GSON报错Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

错误

打开firefox浏览器查看

错误代码

        String data = readJSONData(request);
        Gson gson=new Gson();
        Student studentJson = gson.fromJson(data, Student.class);
        System.out.println(studentJson);
            $.ajax({
                url: "/loginServlet",
                type: "POST",
                data:$("#loginForm").serialize(),
                dataType: "json",
                success: function (data, textStatus) {

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                }
            });

传过去的数据:

原因

GSON的映射实体类错误

并且前端传过来的不是合格的JSON格式数据

解决

对应表单传过来的数据,找到正确映射的实体类对象。

正确代码

        String data = readJSONData(request);
        Gson gson=new Gson();
        LoginUser loginUserJson = gson.fromJson(data, LoginUser.class);
        System.out.println(loginUserJson);
            $.ajax({
                url: "/loginServlet",
                type: "POST",
                data: JSON.stringify({
                    identity: $("#identity").val(),
                    username: $("#username").val(),
                    password: $("#password").val(),
                    verificationCode: $("#verificationCode").val(),
                    rememberMe: $("#rememberMe").val()
                }),
                dataType: "json",
                success: function (data, textStatus) {

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                }
            });

查看前台传过来的数据格式:

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值