json array:
public ActionForward findEmpByDepid(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception { // TODO Auto-generated method stub String depid=request.getParameter("depid"); List<Emp> list=empService.findByDepId(Integer.parseInt(depid)); JsonConfig config=new JsonConfig(); config.registerJsonValueProcessor(Date.class,new JsonValueProcessor() { public Object processObjectValue(String arg0,Object arg1,JsonConfig arg2) { // TODO Auto-generated method stub SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd"); return sd.format((Date)arg1); } public Object processArrayValue(Object arg0,JsonConfig arg1) { // TODO Auto-generated method stub return null; } }); //忽略属性 config.setExcludes(new String[]{"dep"}); JSONArray arr=JSONArray.fromObject(list,config); response.getWriter().print(arr); return null; }
public ActionForward updateEmp(ActionMapping mapping,HttpServletResponse response) throws Exception { String json=request.getParameter("json"); //将字符串转换成对象 JSONObject obj=JSONObject.fromObject(json); //将JsonObject转换成pojo Emp emp=(Emp) JSONObject.toBean(obj,Emp.class); int depid=obj.getInt("depid"); try { empService.update(emp,depid); response.getWriter().print("{result:true}"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); response.getWriter().print("{result:false}"); } return null; }
/** * xml 优点:描述数据比较准确,缺点:数据量大 * json 优点:数据量小, * * json的序列化和反序列化 * json->java对象 反序列化 * java->json 序列化 * json的语法规则 * * * * 单一对象 * {属性1:值1,属性2:值2} * * 数组对象 (重复对象) * [{属性1:值1,属性2:值2},{属性1:值1,属性2:值2}] * 数组不同对象 * {对象名1:{属性1:值1,对象名2:{属性3:值3,属性4:值4}} * */ public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { // String str="{name:'张三',money:100}"; // String str="[{name:'张三',money:100},{name:'李四',money:200}]"; // String str="{stu:{stuname:'张三',stumoney:100},tea:{teaname:'李四',teamoney:200}}"; // {stu:{stuname:'张三',stu:{stuname:'张三',result:true} request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String p=request.getParameter("p"); System.out.println(p); //设置反序列化的日期转换 字符串变对象 JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(new String[] {"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"}) ); /* * json的序列化 * 对象转换成字符串 * * */ Student stu1=new Student("李四",new Date(),200); //获得json的配置 JsonConfig config=new JsonConfig(); //注册一个json对象的转换器,如果转换Date类型,调用该转换器 config.registerJsonValueProcessor(Date.class,new JsonValueProcessor() { public Object processObjectValue(String arg0,JsonConfig arg2) { // TODO Auto-generated method stub if("birthday".equals(arg0)){ SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d=(Date) arg1; return sd.format(d); } return null; } public Object processArrayValue(Object arg0,JsonConfig arg1) { // TODO Auto-generated method stub return null; } }); JSONObject jsonObject=JSONObject.fromObject(stu1,config); //json字符串 System.out.println(jsonObject); List list1=new ArrayList(); JSONArray arr1=JSONArray.fromObject(list1); //json字符串 System.out.println(arr1); //使用json-lib进行对象的反序列化 //转换日期 , //将字符串转换成jsonObject对象 //单一对象 // JSONObject obj=JSONObject.fromObject(p); // //将jsonObject转换成java对象 // Student stu=(Student) JSONObject.toBean(obj,Student.class); // System.out.println(stu.getName()); // System.out.println(stu.getMoney()); // System.out.println(stu.getBirthday()); JSONArray arr=JSONArray.fromObject(p); JsonConfig config1=new JsonConfig(); List<Student> list= JSONArray.toList(arr,Student.class); for (Student student : list) { System.out.println(student.getName()); System.out.println(student.getBirthday()); } } }原文链接:https://www.f2er.com/json/290709.html