国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 舉例講解Java的Jackson庫中ObjectMapper類的使用

舉例講解Java的Jackson庫中ObjectMapper類的使用

2020-03-22 12:24goldensun JAVA教程

這篇文章主要介紹了舉例講解Java的Jackson庫中ObjectMapper類的使用,Jackson庫通常被用來實現Java的對象和JSON之間的轉換功能,需要的朋友可以參考下

ObjectMapper類是Jackson庫的主要類。它提供一些功能將轉換成Java對象匹配JSON結構,反之亦然。它使用JsonParser和JsonGenerator的實例實現JSON實際的讀/寫。

類聲明
以下是org.codehaus.jackson.map.ObjectMapper類的聲明:

?
1
2
3
public class ObjectMapper
  extends ObjectCodec
   implements Versioned

嵌套類

S.N.

類 & 描述

1

static class ObjectMapper.DefaultTypeResolverBuilder//定制TypeResolverBuilder,提供所謂的“默認輸入”使用類型解析構建器(見enableDefaultTyping()了解詳細信息)。

2

static class ObjectMapper.DefaultTyping//使用enableDefaultTyping()枚舉指定什么樣的類型(類)默認輸入應該使用。


構造函數

S.N.

構造函數 & 描述

1

ObjectMapper()//默認的構造函數,這將構建默認JsonFactory必要時使用StdSerializerProvider作為其SerializerProvider,并BeanSerializerFactory作為其SerializerFactory。

2

ObjectMapper(JsonFactory jf)//構造使用指定的JsonFactory構建必要的JsonParsers和/或JsonGenerators映射。

3

ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp)

4

ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp, SerializationConfig sconfig, DeserializationConfig dconfig)

5

ObjectMapper(SerializerFactory sf) 不推薦使用。使用其他構造來代替; 注意,可以設置序列化工廠setSerializerFactory(org.codehaus.jackson.map.SerializerFactory)


這個類繼承了以下類方法:
java.lang.Object

 

例子
測試類基本代碼如下

?
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * @project java
 * @package
 * @file Jackson.java
 * @version 1.0
 
 */
 
public class Jackson {
  /*
   *
   * Class Descripton goes here.
   *
   * @class Jackson
   * @version 1.0
   */
  public static JsonGenerator jsonGenerator = null;
  private static ObjectMapper mapper = new ObjectMapper();
  public static void main(String[] args) {
    Student student = new Student();
    student.setIsstudent(true);
    student.setUid(1000);
    student.setUname("xiao liao");
    student.setUpwd("123");
    student.setNumber(12);
     
    Map<String, Student> stuMap = new HashMap<String, Student>();
    stuMap.put("1", student);
    stuMap.put("2", student);
     
    List<Object> stuList = new ArrayList<Object>();
    List<Student> stuList1 = new ArrayList<Student>();
    stuList1.add(student);
    student= new Student();
    student.setIsstudent(false);
    student.setUid(200);
    student.setUname("xiao mi");
    stuList1.add(student);
     
    stuList.add(student);
    stuList.add("xiao xin");
    stuList.add("xiao er");
    stuList.add(stuMap);
     
    //readJson2List();
    try {
      //readJson2Array();
      //writeArray2Json(array);
      //writeJson2List();
      //writeEntity2Json(student);
      writeJson2Entity();
      //writeMap2Json(stuMap);
      //writeList2Json(stuList1);
       
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
   /**
   *
   * <code>writeEntity2Json</code>
   * @description: TODO(實體類轉換成json)
   * @param object
   * @throws IOException
   */
   public static void writeEntity2Json(Object object) throws IOException {
      mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );
      mapper.writeValue( System.out,object );
      
   }
   /**
   *
   * <code>writeArray2Json</code>
   * @description: TODO(數組轉換成json數組)
   * @param object
   * @throws IOException
   */
   public static void writeArray2Json(Object object) throws IOException {
      
     // writeValue具有和writeObject相同的功能
     mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );
     mapper.writeValue(System.out,object );
      
   }
   /**
   *
   * <code>writeMap2Json</code>
   * @description: TODO(map對象轉換成json對象)
   * @param object
   * @throws IOException
   * @since  2011-11-8   廖益平
   */
   public static void writeMap2Json(Object object) throws IOException {
      
     System.out.println("使用ObjectMapper-----------");
     // writeValue具有和writeObject相同的功能
     System.out.println("==>"+mapper.writeValueAsString(object));
     mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );
     mapper.writeValue( System.out , object );
   }
   /**
   *
   * <code>writeList2Json</code>
   * @description: TODO(list轉換成json)
   * @param object
   * @throws IOException
   */
   public static void writeList2Json(Object object) throws IOException {
     System.out.println("==>"+mapper.writeValueAsString(object));
     mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );
     mapper.writeValue( System.out , object );
   }
   /**
   *
   * <code>writeJson2Entity</code>
   * @description: TODO(json轉換成實體)
   * @throws IOException
   */
   public static void writeJson2Entity() throws IOException {
     System.out.println("json串轉換成entity-------------");
//    File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");
//    FileInputStream inputStream = new FileInputStream(file);
//    Student student = mapper.readValue(inputStream,Student.class);
//    System.out.println(student.toString());
     //漂亮輸出
     //mapper.defaultPrettyPrintingWriter().writeValueAsString(value);
   
     String json = "{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true}";
     Student student1 = mapper.readValue(json,Student.class);
     System.out.println("json2:"+student1.toString());
   }
   /**
   *
   * <code>writeJson2List</code>
   * @description: TODO(json專程list對象)
   * @throws IOException
   */
   public static void writeJson2List() throws IOException {
     System.out.println("json串轉換成entity-------------");
     File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");
     FileInputStream inputStream = new FileInputStream(file);
     Student student = mapper.readValue(inputStream,Student.class);
     System.out.println(student.toString());
      
     String json = "[{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true},{\"uid\":200,\"uname\":\"xiao mi\",\"upwd\":null,\"number\":0.0,\"isstudent\":false}]";
     List<LinkedHashMap<String, Object>> s= mapper.readValue(json,List.class);
     for (int i = 0; i < s.size(); i++) {
       LinkedHashMap<String, Object> link = s.get(i);
       Set<String> key = link.keySet();
       for (Iterator iterator = key.iterator(); iterator.hasNext();) {
        String string = (String) iterator.next();
        System.out.println(string+"==>"+link.get(string));
         
      }
       System.out.println("json:"+i+""+s.get(i).toString());
       
    }
   }
   /**
    * JSON轉換為List對象
    */
   public static void readJson2List() {
    String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"
     + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";
    try {
    List<LinkedHashMap<String, Object>> list = mapper.readValue(
     json, List.class);
    System.out.println(list.size());
    for (int i = 0; i < list.size(); i++) {
     Map<String, Object> map = list.get(i);
     Set<String> set = map.keySet();
     for (Iterator<String> it = set.iterator(); it.hasNext();) {
     String key = it.next();
     System.out.println(key + ":" + map.get(key));
     }
    }
    } catch (JsonParseException e) {
    e.printStackTrace();
    } catch (JsonMappingException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
   }
   /**
    * JSON轉換為List對象
    */
   public static void readJson2Array() {
     String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"
       + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";
     try {
       Student[] students = mapper.readValue(json, Student[].class);
       for (Student student : students) {
        System.out.println(">"+student.toString());
      }
     } catch (JsonParseException e) {
       e.printStackTrace();
     } catch (JsonMappingException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 
}

打印結果 :

?
1
2
3
4
5
6
7
8
9
10
11
串轉換成entity-------------
json2:uid=1000,name=xiao liao,upwd=123,number=12.0,isStudent=true
 
writeMap2Json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}
 
readJson2Array------------------
>uid=1,name=www,upwd=456,number=234.0,isStudent=false
>uid=5,name=tom,upwd=123,number=3.44,isStudent=false
writeMap2Json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}

大家逐個自己試試吧  ,上面也是我的測試代碼

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美簧片在线 | 草久久久 | 国产欧美在线 | 午夜男人视频 | 日韩在线成人 | 久久久久久夜精品精品免费 | 免费日本视频 | 欧美视频在线播放 | 欧美大片免费在线观看 | 国产精品久久久久久久久久免费动 | 国产精品毛片一区二区三区 | 欧美高清一区二区 | 91亚色 | 亚洲免费在线视频 | 日韩福利电影 | 精品成人免费一区二区在线播放 | 欧美一区二区在线刺激视频 | 一区二区三区在线视频播放 | 日韩中文一区二区三区 | 亚洲精品www久久久久久广东 | 色www精品视频在线观看 | 国产精品久久久久久久久久东京 | 九九热在线播放 | 国产精品中文字幕在线 | 亚洲精品在线免费 | 国产精品香蕉在线观看 | 国产日韩欧美视频 | 日韩视频一区二区 | 黄色精品在线 | 欧美一级在线观看 | 黄色片在线播放 | 欧美成人免费在线 | 人人干日日干 | 国产一区二区久久 | 中国大陆高清aⅴ毛片 | 国产精品美女久久久久高潮 | 欧美人妖在线 | 日韩欧美在线观看视频 | 国产激情视频 | 中文字幕av网 | 亚洲第一福利视频 |