溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳談JSON與Javabean轉(zhuǎn)換的幾種形式

發(fā)布時(shí)間:2020-09-04 03:38:55 來源:腳本之家 閱讀:160 作者:jingxian 欄目:編程語言

JSON格式的數(shù)據(jù)傳遞是最常用的方法之一,以下列出了常用的幾種形態(tài)以及與Javabean之間的轉(zhuǎn)換:

String json1="{'name':'zhangsan','age':23,'interests':[{'interest':'籃球','colors':['綠色','黃色']},{'interest':'足球','colors':['紅色','藍(lán)色']}]}";
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}";

首先,此處的轉(zhuǎn)化依賴兩個(gè)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;
  }
  
  
 }
 
}

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

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);
  
 }

2、數(shù)組形式的JSON解析以及GSON解析:

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數(shù)組
  */
 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());
  }
 }

3、內(nèi)嵌JSON形式的JSON與GSON解析:

/**
  * 內(nèi)嵌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);
  }
 }
/**
  * 內(nèi)嵌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);
   }
  }
 }

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

/**
  * 解析一個(gè)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());
  }
 }

5、將一個(gè)JavaBean對象封裝成JSON格式

/**
  * 將一個(gè)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轉(zhuǎn)換成Json
  Gson gson = new Gson();
  String jsonStr = gson.toJson(userBean);
  System.out.println(jsonStr);
  return jsonStr;
 }

}

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

以上這篇詳談JSON與Javabean轉(zhuǎn)換的幾種形式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI