json字符串转java对象

  今天遇到一个问题,前端ajax获取到一个javaBean对象,好多方法发ajax请求需要把这个对象再传到后端,后端再根据这个对象进行操作(之前计划传递id,但发现传递id的话,后端多个方法都需要根据id查询数据库获取这个javaBean,因此就修改为直接传递javaBean),这就涉及到了json字符串与java对象转化,特此记录一下。大概有两种方式比较方便:

  一、Gson的方式

  二、json-lib的方式

1.前端jquery把对象转为json字符串

"dclLine":JSON.stringify(selectedDclLine);

 

 2.后端Gson转java对象

/**
     * 
     * @return
     */
    @RequestMapping("/getDayCharge")
    @ResponseBody
    public List<Object> getDayCharge(HttpServletRequest request){
        Map<String, Object> params = new HashMap<String, Object>(16);
        //前端左侧树回路信息
        String dclLineStr = request.getParameter("dclLine");
        Gson gson = new Gson();
        //Gson转换json字符串到java对象
        DclLine dclLine = gson.fromJson(dclLineStr, DclLine.class);
        params.put("dclLine", dclLine);
        List<Object> result = runningReportsService.getDayCharge(params);
        return result;
    }

转换过程报错:

threw exception [Request processing failed; nested exception is

com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String] with root cause

比较了下json数据与javaBean的类型发现javaBean定义的类型是Double,json数据是"",类型转换报错了

把javaBean中的Double类型改为String类型后,转换成功

但是改javaBean类型涉及改动太大,

在不改变javaBean类型的情况下,试着用json-lib转换了下,发现json-lib转换不报错

 

3.后端使用json-lib把json字符串转为java对象

/**
     * 日用电负荷
     * @return
     */
    @RequestMapping("/getDayCharge")
    @ResponseBody
    public List<Object> getDayCharge(HttpServletRequest request){
        Map<String, Object> params = new HashMap<String, Object>(16);
        //前端左侧树回路信息
        String dclLineStr = request.getParameter("dclLine");
        JSONObject json = JSONObject.fromObject(dclLineStr);
        DclLine dclLine = (DclLine) JSONObject.toBean(json, DclLine.class);
        params.put("dclLine", dclLine);
        List<Object> result = runningReportsService.getDayCharge(params);
        return result;
    }

4.pom配置:

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
            <classifier>jdk15</classifier>
        </dependency>

5.总结:

  Gson:

String dclLineStr = request.getParameter("dclLine");//获取到json字符串
Gson gson = new Gson();//com.google.gson.Gson
//Gson转换json字符串到java对象
DclLine dclLine = gson.fromJson(dclLineStr, DclLine.class);

  json-lib:

String dclLineStr = request.getParameter("dclLine");//获取到json字符串
JSONObject json = JSONObject.fromObject(dclLineStr);//net.sf.json.JSONObject
DclLine dclLine = (DclLine) JSONObject.toBean(json, DclLine.class);
 
 

6.参考博客:

https://blog.csdn.net/chenbin520/article/details/8776915

https://www.cnblogs.com/echo-ling/p/8097698.html

https://blog.csdn.net/a77979744/article/details/53695621

https://blog.csdn.net/tanga842428/article/details/54864485

转载于:https://www.cnblogs.com/ph7seven/p/9264219.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值