提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.StringWriter; |
|
5 |
import java.text.SimpleDateFormat; |
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import com.fasterxml.jackson.core.JsonFactory; |
|
9 |
import com.fasterxml.jackson.core.JsonGenerator; |
|
10 |
import com.fasterxml.jackson.core.JsonParseException; |
|
11 |
import com.fasterxml.jackson.databind.JavaType; |
|
12 |
import com.fasterxml.jackson.databind.JsonMappingException; |
|
13 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
14 |
|
|
15 |
public class Jacksonmethod { |
|
16 |
private static ObjectMapper m=new ObjectMapper(); |
|
17 |
private static JsonFactory jf=new JsonFactory(); |
|
18 |
public static <T> Object fromjson(String jsonstr,Class<T> pojoclass){ |
|
19 |
T pojo=null; |
|
20 |
try { |
|
21 |
pojo=m.readValue(jsonstr, pojoclass); |
|
22 |
} catch (JsonParseException e) { |
|
23 |
System.out.println("jackson解析错误:"+jsonstr); |
|
24 |
} catch (JsonMappingException e) { |
|
25 |
e.printStackTrace(); |
|
26 |
} catch (IOException e) { |
|
27 |
e.printStackTrace(); |
|
28 |
} |
|
29 |
return pojo; |
|
30 |
} |
|
31 |
public static <T> List<T> fromjsontolist(String jsonstr,Class<T> t){ |
|
32 |
//TypeReference<List<T>> typer=new TypeReference<List<T>>(){}; |
|
33 |
if(jsonstr==null) |
|
34 |
return null; |
|
35 |
m.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); |
|
36 |
JavaType javaType = m.getTypeFactory().constructParametricType(List.class, t); |
|
37 |
List<T> pojos=null; |
|
38 |
try { |
|
39 |
pojos=(List<T>)(List<?>)m.readValue(jsonstr, javaType); |
|
40 |
} catch (JsonParseException e) { |
|
41 |
System.out.println("jackson解析错误:"); |
|
42 |
e.printStackTrace(); |
|
43 |
} catch (JsonMappingException e) { |
|
44 |
e.printStackTrace(); |
|
45 |
} catch (IOException e) { |
|
46 |
e.printStackTrace(); |
|
47 |
} |
|
48 |
return pojos; |
|
49 |
} |
|
50 |
public static String tojson(Object pojo, boolean prettyPrint) |
|
51 |
{ |
|
52 |
String str=""; |
|
53 |
StringWriter writer=new StringWriter(); |
|
54 |
try { |
|
55 |
JsonGenerator jg=jf.createGenerator(writer); |
|
56 |
if(prettyPrint) |
|
57 |
jg.useDefaultPrettyPrinter(); |
|
58 |
m.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm")); |
|
59 |
m.writeValue(jg, pojo); |
|
60 |
str=writer.toString(); |
|
61 |
} catch (IOException e) { |
|
62 |
// TODO Auto-generated catch block |
|
63 |
e.printStackTrace(); |
|
64 |
} |
|
65 |
return str; |
|
66 |
} |
|
67 |
public static String tojson_date(Object pojo, boolean prettyPrint) |
|
68 |
{ |
|
69 |
String str=""; |
|
70 |
StringWriter writer=new StringWriter(); |
|
71 |
try { |
|
72 |
JsonGenerator jg=jf.createGenerator(writer); |
|
73 |
if(prettyPrint) |
|
74 |
jg.useDefaultPrettyPrinter(); |
|
75 |
m.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); |
|
76 |
m.writeValue(jg, pojo); |
|
77 |
str=writer.toString(); |
|
78 |
} catch (IOException e) { |
|
79 |
// TODO Auto-generated catch block |
|
80 |
e.printStackTrace(); |
|
81 |
} |
|
82 |
return str; |
|
83 |
} |
|
84 |
} |