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