码迷,mamicode.com
首页 > Web开发 > 详细

使用Jackson操作Json

时间:2017-05-18 23:11:09      阅读:385      评论:0      收藏:0      [点我收藏+]

标签:方式   bin   转换   sys   data   color   com   on()   token   

1.引入jasckson-core.jar,jackson-annotations.jar和jackson-databind.jar

 

2.因为没有做从http获取和提交json数据,所以做个数据类代替

 1 class WxJson {
 2     public String getAccess_token() {
 3         return access_token;
 4     }
 5 
 6     public void setAccess_token(String access_token) {
 7         this.access_token = access_token;
 8     }
 9 
10     public int getExpires_in() {
11         return expires_in;
12     }
13 
14     public void setExpires_in(int expires_in) {
15         this.expires_in = expires_in;
16     }
17 
18     public String getRefresh_token() {
19         return refresh_token;
20     }
21 
22     public void setRefresh_token(String refresh_token) {
23         this.refresh_token = refresh_token;
24     }
25 
26     public String getOpenid() {
27         return openid;
28     }
29 
30     public void setOpenid(String openid) {
31         this.openid = openid;
32     }
33 
34     public String getScope() {
35         return scope;
36     }
37 
38     public void setScope(String scope) {
39         this.scope = scope;
40     }
41 
42     public String getUnionid() {
43         return unionid;
44     }
45 
46     public void setUnionid(String unionid) {
47         this.unionid = unionid;
48     }
49 
50     private String access_token;
51     private int expires_in;
52     private String refresh_token;
53     private String openid;
54     private String scope;
55     private String unionid;
56 }

 

3下面先用两种方式将数据转换成json数据,最后将转换的json数据转换回List对象

 1 import com.fasterxml.jackson.databind.ObjectMapper;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) throws IOException {
10 
11         WxJson wxJson = new WxJson();
12         ObjectMapper mapper = new ObjectMapper();
13 
14         // Convert a java object to json
15         wxJson.setAccess_token("ACCESS_TOKEN");
16         wxJson.setExpires_in(7200);
17         wxJson.setRefresh_token("REFRESH_TOKEN");
18         wxJson.setOpenid("OPENID");
19         wxJson.setScope("SCOPE");
20         wxJson.setUnionid("o6_bmasdasdsad6_2sgVt7hMZOPfL");
21         String json = mapper.writeValueAsString(wxJson);
22         System.out.println(json);
23 
24         List<WxJson> wxJsons = new ArrayList<WxJson>();
25         wxJsons.add(wxJson);
26         String jsonList = mapper.writeValueAsString(wxJsons);
27         System.out.println(jsonList);
28 
29 
30         WxJson wxJson1 = mapper.readValue(json, WxJson.class);
31         System.out.println(wxJson1.getAccess_token() + " " + wxJson1.getExpires_in());
32 
33     }
34 }

 

输出结果

1 {"access_token":"ACCESS_TOKEN","expires_in":7200,"refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE","unionid":"o6_bmasdasdsad6_2sgVt7hMZOPfL"}
2 [{"access_token":"ACCESS_TOKEN","expires_in":7200,"refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE","unionid":"o6_bmasdasdsad6_2sgVt7hMZOPfL"}]
3 ACCESS_TOKEN 7200

 

使用Jackson操作Json

标签:方式   bin   转换   sys   data   color   com   on()   token   

原文地址:http://www.cnblogs.com/mazing/p/6876012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!