JSON與Javabean轉換的幾種形式,jsonjavabean轉換

來源:互聯網
上載者:User

JSON與Javabean轉換的幾種形式,jsonjavabean轉換

    JSON格式的資料傳遞是最常用的方法之一,以下列出了常用的幾種形態以及與Javabean之間的轉換:

  String json1="{'name':'zhangsan','age':23,'interests':[{'interest':'籃球','colors':['綠色','黃色']},{'interest':'足球','colors':['紅色','藍色']}]}";
  String json2="[{'name':'zhangsan'},{'name':'lisi'},{'name':'王五'}]";
  String json3="{'1':{'name':'zhangsan'},'3':{'name':'lisi'},'4':{'name':'wangwu'}}";//map
  String json4="{'name':'zhangsan','age':23}";

首先,此處的轉化依賴兩個JAR包

 1     <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> 2     <dependency> 3         <groupId>com.google.code.gson</groupId> 4         <artifactId>gson</artifactId> 5         <version>2.8.1</version> 6     </dependency> 7     <!-- https://mvnrepository.com/artifact/org.json/json --> 8     <dependency> 9         <groupId>org.json</groupId>10         <artifactId>json</artifactId>11         <version>20170516</version>12     </dependency>
View Code

其次,封裝的Javabean代碼如下

 1 import java.util.List; 2  3 public class UserBean { 4  5     private String name; 6      7     private Integer age; 8      9     private List<InterestBean> interests;10 11     public String getName() {12         return name;13     }14 15     public void setName(String name) {16         this.name = name;17     }18 19     public Integer getAge() {20         return age;21     }22 23     public void setAge(Integer age) {24         this.age = age;25     }26     27     28     29     30     public List<InterestBean> getInterests() {31         return interests;32     }33 34     public void setInterests(List<InterestBean> interests) {35         this.interests = interests;36     }37 38 39 40 41      class InterestBean{42         private String interest;43         44         private List<String> colors;45 46         public String getInterest() {47             return interest;48         }49 50         public void setInterest(String interest) {51             this.interest = interest;52         }53 54         public List<String> getColors() {55             return colors;56         }57 58         public void setColors(List<String> colors) {59             this.colors = colors;60         }61         62         63     }64     65 }
View Code

1、普通的json4格式的JSON解析:

 1 public void testParseJson(){ 2          3         JSONObject jsonObj = new JSONObject(json4); 4         String name = jsonObj.getString("name"); 5         int age = jsonObj.getInt("age"); 6         System.out.println(name); 7         System.out.println(age); 8         UserBean user = new UserBean(); 9         user.setAge(age);10         user.setName(name);11         12     }
View Code

2、數組形式的JSON解析以及GSON解析:

1 public void testJsonArray(){2         JSONArray jsonArray = new JSONArray(json2);3         for (int i = 0; i < jsonArray.length(); i++) {4             JSONObject jsonObj = jsonArray.getJSONObject(i);5             String name = jsonObj.getString("name");6             System.out.println(name);7 8         }9     }
View Code
 1     /** 2      * 解析json數組 3      */ 4     public void testParseListJson(){ 5         Gson gson = new Gson(); 6         Type type = new TypeToken<List<UserBean>>(){}.getType(); 7         List<UserBean> users = gson.fromJson(json2, type); 8         for(UserBean user:users){ 9             System.out.println(user.getName());10         }11     }
View Code

3、內嵌JSON形式的JSON與GSON解析:

 1 /** 2      * 內嵌JSON解析 3      */ 4     public void testParseJson1(){ 5         JSONObject rootJson = new JSONObject(json1); 6         JSONArray jsonInterestArray = rootJson.getJSONArray("interests"); 7         for (int i = 0; i < jsonInterestArray.length(); i++) { 8             JSONObject interestJsonObj = jsonInterestArray.getJSONObject(i); 9             String interest = interestJsonObj.getString("interest");10             System.out.println(interest);11             Object obj = interestJsonObj.get("colors");12             System.out.println(obj);13         }14     }
View Code
 1 /** 2      * 內嵌GSON解析 3      */ 4     public void testSimpleJson(){ 5         Gson gson = new Gson(); 6         UserBean user = gson.fromJson(json1, UserBean.class); 7         System.out.println(user.getName()); 8         System.out.println(user.getAge()); 9         System.out.println(user.getInterests().size());10         List<InterestBean> list = user.getInterests();11         for(InterestBean bean:list) {12             System.out.println(bean.getInterest());13             List<String> colors = bean.getColors();14             for(String color:colors){15                 System.out.println(color);16             }17         }18     }
View Code

4、Map形式的JSON的GSON解析:

 1     /** 2      * 解析一個map類型的json 3      */ 4     public void testParseMapJson(){ 5         Gson gson = new Gson(); 6         Type type = new TypeToken<Map<String,UserBean>>(){}.getType(); 7         Map<String,UserBean> map = gson.fromJson(json3, type); 8         Set<String> keys = map.keySet(); 9         for(String key:keys){10             UserBean bean = map.get(key);11             System.out.println(key);12             System.out.println(bean.getName());13         }14     }
View Code

5、將一個JavaBean對象封裝成JSON格式

 1     /** 2      * 將一個JavaBean對象封裝成JSON格式 3      */ 4     public String testJavaBean2Json(){ 5         UserBean userBean = new UserBean(); 6         userBean.setName("zhangsan"); 7         userBean.setAge(33); 8         List<InterestBean> list = new ArrayList<InterestBean>(); 9         InterestBean bean1 = new UserBean().new InterestBean();10         bean1.setInterest("籃球1");11         InterestBean bean2 = new UserBean().new InterestBean();12         bean2.setInterest("籃球2");13         list.add(bean1);14         list.add(bean2);15         userBean.setInterests(list);16         //將User Bean轉換成Json17         Gson gson = new Gson();18         String jsonStr = gson.toJson(userBean);19         System.out.println(jsonStr);20         return jsonStr;21     }22 23 }
View Code

僅供參考,如有雷同,純屬巧合^_^

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.