java与json互相转换(解决日期问题)
2014-03-19 20:06
525 查看
转载自:http://www.yuuzle.com/java-and-json-conversion-to-resolve-the-question-of-the-date.html
JSON即JavaScript
ObjectNatation,它是一种轻量级的数据交换格式,非常适合于服务器与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
打印结果:list->json:["apple","orange"]
2.Map-》JSON
打印结果:map->json:{"age":30,"name":"Michael","baby":["Lucy","Lily"]}3.bean->JSON
打印结果: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如下:
测试代码:
打印结果:bean->json:{"age":1,"born":"2010-03-10","name":"NewBaby"}这就能得到我们想要的结果了。
4.java->JSON过滤指定的属性
打印结果:bean->json[addpropertyfilter]:{"age":1,"name":"NewBaby"}从执行结果可以看出:born这个属性已经成功过滤掉了。
三、JSON-》java
1.如何把json的yyyy-MM-dd的转换为Bean中的util.Date类型:
打印结果:
1984-01-12
1990-05-01
2.JSON-》List、Map
打印结果:
apple
orange
nameMichael
age30
baby[Lucy,Lily]
返回
JSON即
ObjectNatation,它是一种轻量级的数据交换格式,非常适合于服务器与
的交互。本文主要讲解下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
上面的包可以从下面的连接下载:
二、java-》JSON
1.List-》JSON
1 | List<String> list= new ArrayList<String>(); |
2 | list.add( "apple" ); |
3 | list.add( "orange" ); |
4 | JSONArray jarr=JSONArray.fromObject(list); |
5 | System.out.println( "list->json:" +jarr.toString()); |
2.Map-》JSON
1 | Map<String, Object>map= new HashMap<String,Object>(); |
2 | map.put( "name" , "Michael" ); |
3 | map.put( "baby" , new String[]{ "Lucy" , "Lily" }); |
4 | map.put( "age" , 30 ); |
5 | JSONObject jo=JSONObject.fromObject(map); |
6 | System.out.println( "map->json:" +jo.toString()); |
1 | JsonBean bean= new JsonBean(); |
2 | bean.setName( "NewBaby" ); |
3 | bean.setAge( 1 ); |
4 | bean.setBorn( new Date()); |
5 | jo =JSONObject.fromObject(bean); |
6 | System.out.println( "bean->json:" +jo.toString()); |
上面的例子中你会发现它把bean对象里的util.Date这个类型的所有属性一一转换出来。在实际运用过程中,大多数情况下我们希望能转化为yyyy-MM-dd这种格式,下面就讲一讲如何实现:
首先要写一个新的类JsonDateValueProcessor如下:
1 | /** |
2 | * |
3 | * |
4 | */ |
5 | public class
implements
|
6 |
7 | /** |
8 | * |
9 | */ |
10 | private
"yyyy-MM-dd" ; |
11 |
12 | /** |
13 | * |
14 | */ |
15 | public
|
16 | super (); |
17 | } |
18 |
19 | /** |
20 | * |
21 | */ |
22 | public
|
23 | super (); |
24 | this .datePattern |
25 | } |
26 |
27 | /** |
28 | * |
29 | * |
30 | * |
31 | */ |
32 | public
|
33 | return
|
34 | } |
35 |
36 | /** |
37 | * |
38 | * |
39 | * |
40 | * |
41 | */ |
42 | public
|
43 | JsonConfig |
44 | return
|
45 | } |
46 |
47 | /** |
48 | * |
49 | * |
50 | * |
51 | */ |
52 | private
|
53 | try
|
54 | if (value instanceof
|
55 | SimpleDateFormat new
|
56 | Locale.UK); |
57 | return
|
58 | } |
59 | return
null
""
|
60 | } catch (Exceptione){ |
61 | return "" ; |
62 | } |
63 |
64 | } |
65 |
66 | /** |
67 | * |
68 | */ |
69 | public
|
70 | return
|
71 | } |
72 |
73 | /** |
74 | * |
75 | */ |
76 | public void
|
77 | datePattern |
78 | } |
79 |
80 | } |
1 | JsonBean bean= new JsonBean(); |
2 | bean.setName( "NewBaby" ); |
3 | bean.setAge( 1 ); |
4 | bean.setBorn( new Date()); |
5 |
6 | JsonConfig new
|
7 | jsonConfig.registerJsonValueProcessor(Date. class , |
8 | new
|
9 |
10 | JSONObject |
11 | System.out.println( "bean->json:" +jo.toString()); |
4.java->JSON过滤指定的属性
1 | JsonBean bean= new JsonBean(); |
2 | bean.setName( "NewBaby" ); |
3 | bean.setAge( 1 ); |
4 | bean.setBorn( new Date()); |
5 | jo =JSONObject.fromObject(bean); |
6 |
7 | JsonConfig new
|
8 |
9 | PropertyFilter new
|
10 | public boolean
|
11 | if ( "born" .equals(name)) |
12 | return true ; |
13 | } |
14 | return false ; |
15 | } |
16 | }; |
17 | jsonConfig.setJsonPropertyFilter(filter); |
18 |
19 | njo |
20 | System.out.println( "bean->json |
21 | +njo.toString()); |
三、JSON-》java
1.如何把json的yyyy-MM-dd的转换为Bean中的util.Date类型:
1 | JSONUtils.getMorpherRegistry().registerMorpher( |
2 | new DateMorpher( new String[]{ "yyyy-MM-dd"
|
3 |
4 | String "[{\"name\": ; |
5 |
6 | Collection<JsonBean>list=JSONArray.toCollection(JSONArray |
7 | .fromObject(jsonStr),JsonBean. class ); |
8 | //DateUtil.getFormatDate(date,fmtstr)日期转字符串这里不再写代码了 |
9 | for (JsonBeano:list){ |
10 | System.out.println(DateUtil |
11 | .getFormatDate(o.getBorn(), "yyyy-MM-dd" )); |
12 | } |
1984-01-12
1990-05-01
2.JSON-》List、Map
1 | String "[\"apple\",\"orange\"]" ; |
2 | Collection<String>strlist=JSONArray.toCollection(JSONArray |
3 | .fromObject(listStr)); |
4 | for (Stringstr:strlist){ |
5 | System.out.println(str); |
6 | } |
7 |
8 | String "{\"age\":30,\"name\":\"Michael\",\"baby\":[\"Lucy\",\"Lily\"]}" ; |
9 | Map<String, Object>map=(Map)JSONObject.toBean(JSONObject |
10 | .fromObject(mapStr),Map. class ); |
11 | for (Entry<String,Object>entry:map.entrySet()){ |
12 | System.out.println(entry.getKey() " "
|
13 | } |
apple
orange
nameMichael
age30
baby[Lucy,Lily]
返回
相关文章推荐
- java与json互相转换(解决日期问题)
- java与json互相转换(解决日期问题),js显示json数据时出现日期类型显示[object,object]现象解决!
- java与json互相转换(解决日期问题)
- java与json互相转换(解决日期问题) 【转】http://hi.baidu.com/nwwolfhdy/blog/item/54e0ac13a6282f0e5baf53f5.html
- java与json互相转换(解决日期问题)
- java与json互相转换(解决日期问题)
- java与json互相转换(解决日期问题)
- JAVA与JSON日期互相转换(解决日期和去除属性题目)
- 解决Java中Long类型转换成Json时,在Js代码调用时精度丢失的问题
- JSON(3):Java的Date类型转换为符合json语法的字符串遇到的问题及其解决办法
- Java 用Jackson进行json和object之间的转换(并解决json中存在新增多余字段的问题)
- 关于如何处理JSONObject.fromObject(Object obj)无法转换特殊日期(java.sql.Date,java.sql.Timestamp)格式的问题。
- 工具类 json,pojo,text互相转换,解决浏览器对json的兼容问题
- 关于java中long类型的数据转换json传到前台时丢失精度问题的解决办法
- Java 用Jackson进行json和object之间的转换(并解决json中存在新增多余字段的问题)
- 解决日期转换json 报错问题
- 关于如何处理JSONObject.fromObject(Object obj)无法转换特殊日期(java.sql.Date,java.sql.Timestamp)格式的问题。
- Java中使用json时java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher问题解决
- java生成json时产生栈溢出错误问题的解决
- jsontools实现java对象与json值之间互相转换