java与json互相转换(解决日期和去除属性题目) JSON 即 JavaScript Object Natation,它是一种轻量级的数据互换格局,很是合适于办事器与 JavaScript 的交互。本文首要讲解下java和JSON之间的转换,希罕是解决互相转换碰到日期题目和指定属性的过滤。 一、须要相干的jar包: json-lib-xxx.jar ezmorph-xxx.jar commons-httpclient-xxx.jar commons-lang-xxx.jar commons-logging-xxx.jar commons-collections-xxx.jar 上方的包可以从下面的连接: http://commons.apache.org/index.html http://json-lib.sourceforge.net http://ezmorph.sourceforge.net 二、java-》JSON 1.List-》JSON Java代码 List list = new ArrayList(); list.add("apple"); list.add("orange"); JSONArray jarr = JSONArray.Object(list); System.out.println("list->json:" + jarr.toString()); 打印成果:list->json:["apple","orange"] 2.Map-》JSON Java代码 Map map = new HashMap(); map.put("name", "Michael"); map.put("baby", new String[] { "Lucy", "Lily" }); map.put("age", 30); JSONObject jo = JSONObject.Object(map); System.out.println("map->json:" + jo.toString()); 打印成果:map->json:{"age":30,"name":"Michael","baby":["Lucy","Lily"]} 3.bean->JSON Java代码 JsonBean bean = new JsonBean(); bean.setName("NewBaby"); bean.setAge(1); bean.setBorn(new Date()); jo = JSONObject.Object(bean); System.out.println("bean->json:" + jo.toString()); 打印成果:bean->json:{"age":1,"born":{"date":10,"day":3,"hours":14,"minutes":14,"month":2,"seconds":1,"time":1268201641228,"timezoneOffset":-480,"year":110},"name":"NewBaby"} 4.bean->JSON 日期转换 上方的例子中你会发明它把bean对象里的util.Date这个类型的所有属性一一转换出来。在实际应用过程中,大多半景象下我们能转化为yyyy-MM-dd这种格局,下面就讲一讲如何实现: 起首要写一个新的类JsonDateValueProcessor如下: Java代码 /** * JSON 日期格局处理惩罚(java转化为JSON) * @author Michael sun */ public class JsonDateValueProcessor implements JsonValueProcessor { /** * datePattern */ private String datePattern = "yyyy-MM-dd"; /** * JsonDateValueProcessor */ public JsonDateValueProcessor() { super(); } /** * @param format */ public JsonDateValueProcessor(String format) { super(); this.datePattern = format; } /** * @param value * @param jsonConfig * @return Object */ public Object processArrayValue(Object value, JsonConfig jsonConfig) { return process(value); } /** * @param key * @param value * @param jsonConfig * @return Object */ public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { return process(value); } /** * process * @param value * @return */ private Object process(Object value) { try { if (value instanceof Date) { SimpleDateFormat sdf = new SimpleDateFormat(datePattern, Locale.UK); return sdf.format((Date) value); } return value == null ? "" : value.toString(); } catch (Exception e) { return ""; } } /** * @return the datePattern */ public String getDatePattern() { return datePattern; } /** * @param pDatePattern the datePattern to set */ public void setDatePattern(String pDatePattern) { datePattern = pDatePattern; } } 测试代码: Java代码 JsonBean bean = new JsonBean(); bean.setName("NewBaby"); bean.setAge(1); bean.setBorn(new Date()); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor()); JSONObject jo = JSONObject.Object(bean, jsonConfig); System.out.println("bean->json:" + jo.toString()); 打印成果:bean->json:{"age":1,"born":"2010-03-10","name":"NewBaby"} 这就能获得我们想要的成果了。 4.java->JSON 过滤指定的属性 Java代码 JsonBean bean = new JsonBean(); bean.setName("NewBaby"); bean.setAge(1); bean.setBorn(new Date()); jo = JSONObject.Object(bean); JsonConfig jsonConfig = new JsonConfig(); PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if ("born".equals(name)) { return true; } return false; } }; jsonConfig.setJsonPropertyFilter(filter); njo = JSONObject.Object(bean, jsonConfig); System.out.println("bean->json [add property filter] :" + njo.toString()); 打印成果:bean->json [add property filter] :{"age":1,"name":"NewBaby"} 从履行成果可以看出:born 这个属性已经成功过滤掉了。 三、JSON-》java 1.如何把json的yyyy-MM-dd的转换为Bean中的util.Date类型: Java代码 JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(new String[] { "yyyy-MM-dd" })); String jsonStr = "[{"name": "husband", "age": "26", "born": "1984-01-12"},{"name": "wife", "age": "20", "born": "1990-05-01"}]"; Collection list = JSONArray.toCollection(JSONArray .Object(jsonStr), JsonBean.class); //DateUtil.getFormatDate(date,fmtstr)日期转字符串这里不再写代码了 for (JsonBean o : list) { System.out.println(DateUtil .getFormatDate(o.getBorn(), "yyyy-MM-dd")); } 打印成果: 1984-01-12 1990-05-01 2. JSON-》List、 Map Java代码 String listStr = "["apple","orange"]"; Collection strlist = JSONArray.toCollection(JSONArray .Object(listStr)); for (String str : strlist) { System.out.println(str); } String mapStr = "{"age":30,"name":"Michael","baby":["Lucy","Lily"]}"; Map map = (Map) JSONObject.toBean(JSONObject .Object(mapStr), Map.class); for (Entry entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } 打印成果: apple orange name Michael age 30 baby [Lucy, Lily]
json日期转换java日期_JAVA与JSON日期互相转换(解决日期和去除属性题目)
最新推荐文章于 2023-03-17 19:43:59 发布