package com.hisunsray.gw.dao.impl; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; public class JsonDateValueProcessor implements JsonValueProcessor { private String format ="yyyy-MM-dd"; public Object processArrayValue(Object value, JsonConfig config) { return process(value); } public Object processObjectValue(String key, Object value, JsonConfig config) { return process(value); } private Object process(Object value){ if(value instanceof Date){ SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.UK); return sdf.format(value); } return value == null ? "" : value.toString(); } public static void main(String[] args) { // List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); // Map<String, Object> s1 = new HashMap<String, Object>(); // s1.put("name", "jim"); // s1.put("age", "15"); // s1.put("time", new Date()); // list.add(s1); // Map<String, Object> s2 = new HashMap<String, Object>(); // s2.put("name", "lucy"); // s2.put("age", "12"); // s2.put("time", new Date()); // list.add(s2); List list =new ArrayList(); list.add("11"); list.add(new Date()); System.out.println(list); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor()); JSONArray jo = JSONArray.fromObject(list, jsonConfig); JSONObject jo2=new JSONObject(); jo2.put("11", jo); System.out.println("json:" + jo2); } }
Date格式转换成json
猜你喜欢
转载自songywlanna.iteye.com/blog/2238382
今日推荐
周排行