使用Jackson操作Json

來源:互聯網
上載者:User

標籤:方式   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 json15         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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.