大大的芒果 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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。