JSONObject的内容为:

{"userId":"A000004FFDCE14","userName":"好好干g","userMp":"1820257596","clientName":"风格","regDate":"2016-07-05 00:00:00","startDate":"2016-07-05 00:00:00","endDate":"2017-09-05 00:00:00","times":15458,"master":"master","yesorno":3}]

 

List<PreparedInfo> listpre = PreApplyConvertEntity.secectlistinfo();

			Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Date.class, new DateTypeAdapter())
					.setDateFormat("yyyy-MM-dd HH:mm:ss").create();

			String json = gson.toJson(listpre);
			System.out.println(json);

			JSONArray ja = JSONArray.fromObject(json);
			for (int i = 0; i < ja.size(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				String[] dateFormats = new String[] {"yyyy-MM-dd HH:mm:ss" };
				JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
				PreparedInfo person = (PreparedInfo) JSONObject.toBean(jo, PreparedInfo.class);
				System.out.println(person);

 

 1 DateTypeAdapter类:
 2     public class DateTypeAdapter implements JsonSerializer<Date> ,JsonDeserializer<Date> {
 3     private final SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 4     @Override
 5     public Date deserialize(JsonElement json, Type date, JsonDeserializationContext context) throws JsonParseException {
 6         // TODO Auto-generated method stub
 7         if (!(json instanceof JsonPrimitive)) {  
 8             throw new JsonParseException("The date should be a string value");  
 9         }  
10   
11         try {  
12             return  dateFormate.parse(json.getAsString()); 
13             
14             
15         } catch (ParseException e) {  
16             throw new JsonParseException(e);  
17         }  
18      
19         
20         
21     }
22 
23     @Override
24     public JsonElement serialize(Date src, Type arg1, JsonSerializationContext arg2) {
25         String dateFormatAsString = dateFormate.format(new Date(src.getTime()));  
26         return new JsonPrimitive(dateFormatAsString);  
27         
28     }
29   
30 }
View Code

相关文章:

  • 2021-07-26
  • 2022-01-31
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-10
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案