大大的芒果 2017-02-26 10:02 采纳率: 33.3%
浏览 3310
已结题

fastjson解析http的get 参数,转换成java对象

我的http请求头如下

 http://localhost:8080/trace/Weather?temperatureMax=22&temperatureMin=1&humedityMax=52&humedityMin=2

当我请求后,json会得到一个这个

 weather = {"temperatureMax":["11"],"temperatureMin":["2"],"humidityMax":["55"],"humidityMin":["2"]} 

如果我使用parstObject()转换 weather java对象会报出一个异常"["无法转换
大概的意思是我想把 最大温度11 转到weather 对象中但是[11] 他当作一个整体.
如果我一个一个取出来使用Weather构造函数创造一个实例.
那么就成了一个数组

 jsonObject.getInteger("temperatureMax");//出现 [ 问题
 jsonObject.get("tempertureMax"); //得到的是一个String数组但是只有一个数据.

我最后处理办法

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        @SuppressWarnings("unchecked")
        JSONObject jsonWeather = new JSONObject(request.getParameterMap());

//      GardenWeather weather = new GardenWeather(jsonWeather.getInteger("temperatureMax"), jsonWeather.getInteger("temperatureMin")
//              , jsonWeather.getInteger("humidityMax"), jsonWeather.getInteger("humidityMin"));
//      System.out.println(weather.toString());

        log.info("weather = " + jsonWeather.toJSONString());

        response.getWriter().write("temperatureMax = " + jsonWeather.toJSONString() );
        response.getWriter().println();
        String[] max =  (String[]) jsonWeather.get("temperatureMax");
        response.getWriter().write(new Integer(max[0]).toString());

        weatherService.insertWeather();
    }

请问有没有更好的办法可以把http中的get请求转换成jave对象

  • 写回答

3条回答 默认 最新

  • oyljerry 2017-02-26 11:55
    关注

    你这个json不是一个标准格式。中括号的地方应该是没有就估计可以直接转换。如果可以修改返回json,就修改一下格式。不然就只能自己解析

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作