JSONARRAY

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());
		}
		
	}

}

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...