下面小編就為大家帶來一篇詳談JSON與Javabean轉換的幾種形式。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧
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套件
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20170516</version> </dependency>
其次,封裝的Javabean程式碼如下
#
import java.util.List; public class UserBean { private String name; private Integer age; private List<InterestBean> interests; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public List<InterestBean> getInterests() { return interests; } public void setInterests(List<InterestBean> interests) { this.interests = interests; } class InterestBean{ private String interest; private List<String> colors; public String getInterest() { return interest; } public void setInterest(String interest) { this.interest = interest; } public List<String> getColors() { return colors; } public void setColors(List<String> colors) { this.colors = colors; } } }
#
public void testParseJson(){ JSONObject jsonObj = new JSONObject(json4); String name = jsonObj.getString("name"); int age = jsonObj.getInt("age"); System.out.println(name); System.out.println(age); UserBean user = new UserBean(); user.setAge(age); user.setName(name); }
public void testJsonArray(){ JSONArray jsonArray = new JSONArray(json2); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); String name = jsonObj.getString("name"); System.out.println(name); } }
/** * 解析json数组 */ public void testParseListJson(){ Gson gson = new Gson(); Type type = new TypeToken<List<UserBean>>(){}.getType(); List<UserBean> users = gson.fromJson(json2, type); for(UserBean user:users){ System.out.println(user.getName()); } }
/** * 内嵌JSON解析 */ public void testParseJson1(){ JSONObject rootJson = new JSONObject(json1); JSONArray jsonInterestArray = rootJson.getJSONArray("interests"); for (int i = 0; i < jsonInterestArray.length(); i++) { JSONObject interestJsonObj = jsonInterestArray.getJSONObject(i); String interest = interestJsonObj.getString("interest"); System.out.println(interest); Object obj = interestJsonObj.get("colors"); System.out.println(obj); } }
/** * 内嵌GSON解析 */ public void testSimpleJson(){ Gson gson = new Gson(); UserBean user = gson.fromJson(json1, UserBean.class); System.out.println(user.getName()); System.out.println(user.getAge()); System.out.println(user.getInterests().size()); List<InterestBean> list = user.getInterests(); for(InterestBean bean:list) { System.out.println(bean.getInterest()); List<String> colors = bean.getColors(); for(String color:colors){ System.out.println(color); } } }
/** * 解析一个map类型的json */ public void testParseMapJson(){ Gson gson = new Gson(); Type type = new TypeToken<Map<String,UserBean>>(){}.getType(); Map<String,UserBean> map = gson.fromJson(json3, type); Set<String> keys = map.keySet(); for(String key:keys){ UserBean bean = map.get(key); System.out.println(key); System.out.println(bean.getName()); } }
/** * 将一个JavaBean对象封装成JSON格式 */ public String testJavaBean2Json(){ UserBean userBean = new UserBean(); userBean.setName("zhangsan"); userBean.setAge(33); List<InterestBean> list = new ArrayList<InterestBean>(); InterestBean bean1 = new UserBean().new InterestBean(); bean1.setInterest("篮球1"); InterestBean bean2 = new UserBean().new InterestBean(); bean2.setInterest("篮球2"); list.add(bean1); list.add(bean2); userBean.setInterests(list); //将User Bean转换成Json Gson gson = new Gson(); String jsonStr = gson.toJson(userBean); System.out.println(jsonStr); return jsonStr; } }
以上是Javabean與JSON轉換的幾種形式介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!