json字符串转java对象

2018-07-06 01:24:22来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  今天遇到一个问题,前端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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:性能优化2--内存优化

下一篇:Spring @Cacheable 缓存不生效的问题