JSONObject jsonObject = null;
try{
jsonObject = JSONObject.fromObject(jsonStr);
}catch(Exception e){
loger.error("json字符串格式不对,转换json对象失败 {}"+e);
}
TestModel model = (TestModel)JSONObject.toBean(jsonObject, TestModel.class);
System.out.println(model.getName());
=======================================================================
//list2json
List<ZonePvByHour> hours = new ArrayList<ZonePvByHour>();
for(int i=0; i<2; i++){
ZonePvByHour hour = new ZonePvByHour();
hour.setDate("2013-09-09");
hour.setDate("2013-09-10");
hour.setPv(111);
hours.add(hour);
}
System.out.println(JSONArray.fromObject(hours).toString());
json2list
String jsonstr = [{"date":"2013-09-10","hour":"","id":0,"pv":111,"userId":0},{"date":"2013-09-10","hour":"","id":0,"pv":111,"userId":0}]
JSONArray aa = JSONArray.fromObject(jsonstr);
Collection c = JSONArray.toCollection(aa, ZonePvByHour.class);
Iterator i = c.iterator();
while(i.hasNext()){
ZonePvByHour zone = (ZonePvByHour)i.next();
System.out.println(zone.getDate());
System.out.println(zone.getPv());
}