我的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对象